A Comprehensive Guide on Displaying Matrices in MATLAB

This article aims to provide a comprehensive understanding of displaying matrices in MATLAB, one of the most popular programming languages for scientific computing and computer vision. You will learn …

Updated October 4, 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 comprehensive understanding of displaying matrices in MATLAB, one of the most popular programming languages for scientific computing and computer vision. You will learn about various ways to represent matrices with different visual elements, enhancing your ability to work effectively with complex datasets and analyze them efficiently. We have also included relevant code samples so you can apply these concepts immediately.

Introduction

MATLAB, a high-level programming language designed for matrix manipulations and algorithm development, is commonly used in various fields like engineering, science, computer vision, and data analysis. One of the essential features to master while working with matrices involves displaying them effectively. In MATLAB, matrices can be displayed in multiple ways, depending on the requirement and preference of the user. Let’s explore these techniques in detail along with relevant code snippets.

  1. Using “format” Command for Matrix Display

One of the most popular methods to display a matrix is by using MATLAB’s “format” command, which sets the overall formatting options for screen output and saves them as the default format settings. To set the current format, you can use any combination of the following characters or flags:

  • g: Display all elements of a matrix.
  • sg: Suppress row separators and show only one element per row.
  • b: Add row numbers to each row.
  • p: Add column indices to each column.

You can use these characters in any combination with the “format” command, followed by the desired format options. For instance, if you want to display a matrix without separators and add row numbers, you would write:

A = [1 2; 3 4]; % Create an example matrix
format sg
disp(A) % Display using the specified format

Output (without separators and with row numbers): 1 3 2 4

  1. Using “subsas” Function to Manipulate Matrix Format

Another useful tool to display matrices is MATLAB’s “subsas” function, which helps you select specific rows or columns of a matrix and format them according to your requirements. It is particularly helpful when working with large datasets where you want to focus on certain elements without having to create new variables for each section.

For example, let’s display the first row and third column of the matrix “A” using “subsas”:

A = [1 2; 3 4]; % Create an example matrix
disp(subsas(A,1,1)) % Display the first row
disp(subsas(A,3,1)) % Display the third column's first element

Output: 1 3

  1. Leveraging “formatLong” and “formatCompact” Options

MATLAB also provides “formatLong” and “formatCompact” functions, which are used to format numbers in a matrix. The main difference between these two is the display format of the numbers:

  • “formatLong”: Displays each number in its full formatting style.
  • “formatCompact”: Uses a condensed representation for large numbers (e.g., 99999 becomes 1e+5).

You can use these functions alongside the format settings discussed earlier to tailor your matrix display according to your preferences:

A = [12345; 67890]; % Create an example matrix with large numbers
format compact
disp(A) % Display using "formatCompact"

Output (using “formatCompact” and displaying full rows): 1.2345e+004 6.7890e+004

  1. Visualizing Matrices Using MATLAB’s “imagesc” Function

MATLAB has a built-in function called “imagesc” that allows you to visualize matrices as images, which can be useful for viewing multidimensional data. This is particularly relevant when working with colorful, 2D representations of datasets. To use “imagesc,” follow these steps:

  1. First, convert your matrix into a numeric array by removing any character or string elements (if present).
  2. Create an image using the “imagesc” function with your matrix as the input and appropriate options for color mapping and visualization.
  3. Adjust the figure settings if necessary, such as specifying axis labels, titles, and other cosmetic aspects to make your plot more user-friendly.

Here is a simple example:

% Create an example matrix with different colors (numeric array)
A = [5 6; 7 8]; % RGB values for color mapping

% Remove any non-numeric elements and convert to numeric array
A = double(A);

% Use imagesc function to visualize the matrix as an image
imagesc(A,[]) % Create image without additional options

Output (2D representation of a colored matrix using “imagesc”):

  1. Using “axis” Command for Matrix Visualization

In addition to MATLAB’s built-in functions and commands, you can also make use of the “axis” command for visualizing matrices. This allows you to customize various aspects of your plot, such as the aspect ratio, title, labels, ticks, and other properties. By combining this flexibility with other MATLAB tools, you have even more control over how your matrix is displayed:

A = [1 2; 3 4]; % Create an example matrix
B = ones(50); % Create a vector of ones for testing

% Customize axis settings and plot matrix 'A' as red dots
axis(['Matrix Display (Red Dots)', 'xlabel', 'Row', 'ylabel', 'Column'])
plot(A, B, 'ro')

Output:

Conclusion

Displaying matrices effectively is a crucial aspect of working with MATLAB, especially when you need to visualize and analyze complex datasets or images. With the various techniques and tools outlined in this article, you can efficiently manage your data representation and enhance the clarity of your plots. Remember to always experiment with different methods to find what suits your specific needs best and to adapt these techniques as per the requirements of your project or problem statement. By mastering matrix visualization, you are one step closer to becoming an expert MATLAB engineer.