Learning to Print a Matrix in MATLAB - A Comprehensive Guide

This article aims to provide a thorough understanding of how to print a matrix in MATLAB. We will cover the significance of matrices, various ways of printing them using different formats, how they ar …

Updated November 17, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

This article aims to provide a thorough understanding of how to print a matrix in MATLAB. We will cover the significance of matrices, various ways of printing them using different formats, how they are used for solving complex problems, and code samples to demonstrate each concept. So, let’s dive into the world of MATLAB matrices and their printing techniques!

Introduction to Matrices in MATLAB

Matrices are important data structures in MATLAB as they represent arrays of numbers organized in rows and columns. In MATLAB, you can work with two-dimensional (2D) or multi-dimensional matrices that contain elements arranged like a grid. These matrices play a crucial role in solving numerous mathematical, scientific, and engineering problems through linear algebraic operations such as matrix addition, subtraction, multiplication, division, transposition, and more.

To access MATLAB’s powerful features, you need to familiarize yourself with printing matrices to visualize their content conveniently. In this tutorial, we will explore different methods of printing matrices in MATLAB along with a brief overview of its usage across various fields.

Understanding Matrix Printing Methods in MATLAB

There are several ways to display the contents of a matrix in MATLAB: format specifiers and custom functions. We will break down each method in detail, explaining their advantages and applications.

Format Specifiers in MATLAB

Format specifiers are commonly used within MATLAB for printing matrices or performing operations on them. Here is an overview of the most frequently utilized format specifiers:

  1. format long - This command provides a full-sized matrix print with a column header and row indexes enclosed in square brackets. The default format is also known as 'compact', which displays matrices without column headers but with their row indices.
  2. format short - Employs a shorthand version of the 'compact' format, where the row indices are printed only for odd-numbered rows. This reduces clutter while printing the matrix and is especially useful when working with large matrices.
  3. format long e - Prints a matrix in the 'long' format with exponential notation used to display the numerical values of large or very small numbers more concisely and readably.
  4. format short e - Uses the shorthand 'short' format for matrices while incorporating the exponential notation, similar to the previous two options.
  5. format bank - Formats the matrix print with currency-like display format by showing monetary signs alongside numeric values. This option is best suited for financial and economic data representation.
  6. format rat - Prints a matrix in ratio format, displaying a row vector as the divisor followed by the elements of the corresponding column vector. This is helpful when performing division operations on matrices or vectors.
  7. format compact - Displays a matrix without any column headers and with only even-numbered row indices shown, reducing clutter for better readability. This is often used as the default print format.
  8. format comma - Prints each matrix element separated by commas, useful when exporting data to CSV files or using a comma-separated list of values.
  9. format rat2 - Uses a modified version of the rat format with all elements shown as the quotient and divisor parts in the same row for numerators and denominators. This format is useful when working with fractions, which are often represented as two-element vectors.
  10. format pic - Prints matrix elements using fixed precision to the given number of significant digits, ensuring numerical data is displayed with greater accuracy.

Custom Functions for Matrix Printing in MATLAB

In addition to format specifiers, we can create custom functions to handle various matrix printing scenarios. Here are a couple of examples:

  1. printMatrix(A) - This function can be used to print the contents of any given matrix (A). You can customize this function according to your preferences, such as adjusting font size, colors, or using different delimiters for the elements.
  2. prettyPrint(A) - Another option is creating a pretty-print function that formats and arranges the output of any given matrix in a more visually appealing way. This can include reorganizing the rows and columns to improve readability, using colors, adding borders, or customizing other formatting options.

Usage Examples with Code Samples

To better understand the usage of these various printing methods and functions, let’s consider a sample matrix containing elements from a simple data set:

A = [1 2; 3 4];

Now, we will demonstrate how to use different format specifiers, custom functions, and other techniques to print this matrix in MATLAB.

Applying Format Specifiers

We can apply the format commands to print matrix A using various formats:

  1. Format ‘long’:

    format long; A
    
  2. Format ‘short’:

    format short; A
    
  3. Format ‘long e’ with exponential notation:

    format long e; A
    
  4. Format ‘short e’ with shorthand and exponential notation:

    format short e; A
    
  5. Format ‘bank’ for currency-like display:

    format bank; A
    
  6. Format ‘rat’ for matrix division representation:

    format rat; A
    
  7. Format ‘compact’ (default) with even indices and no column headers:

    format compact; A
    
  8. Format ‘comma’ with commas separating elements:

    format comma; A
    
  9. Format ‘rat2’ for displaying elements as quotient and divisor:

    format rat2; A
    
  10. Format ‘pic’ for fixed precision numeric values:

    format pic; A
    

Using Custom Functions

We can define a simple printMatrix function to print the contents of any given matrix (A):

function printMatrix(A)
disp('Matrix:'); disp(A); end

% Calling the function with Matrix A as input
printMatrix(A)

Output:

Matrix:
    1     2
    3     4

Similarly, we can create a prettyPrint function to format the output of matrix A in an aesthetically pleasing manner:

function prettyPrint(A)
% Add custom formatting logic here, like changing font size or adding borders
% and coloring cells according to values
disp('Pretty-printed Matrix:'); disp(A); end

% Calling the function with Matrix A as input
prettyPrint(A)

Output:

Pretty-printed Matrix:
    1     2
    3     4

Combining Different Techniques for Flexibility

In some cases, you might want to combine the capabilities of format specifiers and custom functions. You can use both approaches in your scripts to achieve the desired matrix printing output based on your requirements. For instance:

  1. Print the matrix A using a format command (e.g., short) followed by applying a custom function (like printMatrix) for further formatting changes.
  2. Create a new format specifier combining multiple options such as format bank,comma, or use different custom functions based on the type of output needed. The possibilities are endless, allowing you to tailor your approach depending on the specific requirements of your MATLAB scripting tasks.

Conclusion

Printing matrices in MATLAB is essential for understanding their content and visualizing data representation. This article has introduced various methods for accomplishing this task using format specifiers and custom functions. By leveraging these techniques, you can effectively print matrices in different formats, making it easier to work with them within your MATLAB environment. Keep practicing and experimenting with the available options to find the method that suits your needs best.