# Mastering MATLAB’s Approach to Matrix Visualization

This article will delve into the world of matrix display in MATLAB, a powerful programming language used for technical computing. With its roots stemming from the MATrix LABoratory, it is an essential …

Updated November 28, 2023

*Hey! If you love Computer Vision and OpenCV as much as I do let's connect on Twitter or LinkedIn. I talk about this stuff all the time and build cool projects.*This article will delve into the world of matrix display in MATLAB, a powerful programming language used for technical computing. With its roots stemming from the MATrix LABoratory, it is an essential tool for data analysis, mathematical computations, and computer vision applications. The tutorial aims to provide you with the necessary knowledge on how matrices are represented in MATLAB, their various display options and features, and how these features can be utilized in your programming journey.

### Introduction to Matrices and Matrix Display in MATLAB

Matrices play a vital role in computational applications such as data manipulation, image processing, linear algebra, and scientific computing. These rectangular or square arrays are composed of numerical values arranged in rows and columns. In MATLAB, matrices form the backbone for solving mathematical problems, analyzing data, and creating computer vision algorithms.

One of the essential features to understand while working with matrices is how they are displayed within the MATLAB environment. To start off, let’s quickly review a few basic terms related to matrices in MATLAB:

- Row Vector - A 1 x n matrix with one row and multiple columns.
- Column Vector - An n x 1 matrix with multiple rows and one column.
- Scalar (or 1x1 Matrix) - A special case of a single element in both row and column, representing a number or scalar value.
- Square Matrix - A matrix where the number of rows is equal to the number of columns.
- Rectangular Matrix - A matrix with unequal numbers of rows and columns.

Understanding Matrix Display Options in MATLAB

There are several built-in display options available in MATLAB for visualizing matrices, catering to various requirements and preferences. These include:

- Default Display: MATLAB automatically renders a matrix in the default layout when you type it in the Command Window or assign it to a variable. This includes rounding the values to the nearest integer and displaying only decimal places (if necessary). Additionally, MATLAB prints row labels as numbers starting from 1 by default.
- Pretty Display: To make your matrices more visually appealing, you can use the pretty function, which formats the data with better spacing for readability. This formatting is usually preferred while working on large and complex matrices.
- Format Specifiers: To display a matrix in a specific format, you can use various format specifiers within the MATLAB environment. These include rounding to certain decimal places or converting to exponential notation. For instance, you could use ‘2.0f’ for rounding up to 2 decimal places and ‘e’ for displaying numbers in scientific notation.
- Format Function: This function allows you to specify a format string, applying that format directly to the matrix elements when displayed. It can be used alongside the pretty function for customizing matrix visualization even further.
- Print Functions: MATLAB also offers print functions like ‘format long’, ‘format short’, and ‘format compact’ which are designed to display matrices in a specific manner, depending on the complexity of the data or your personal preferences.
- Custom Formatting: With custom formatting strings, you can define your preferred layout for matrix visualization. This might involve adding line breaks for better readability or altering font sizes within the output.
- Display Options: MATLAB also provides some options to modify display settings such as font size, font family, and even color schemes. These options can be adjusted through the ‘set’ command to tailor your matrix visualization experience.

Implementing Matrix Display in MATLAB with Examples

To better understand how these display features work, let’s explore a couple of examples that demonstrate their functionality:

Example 1 - Default and Pretty Matrix Displays

Let’s create a 3x3 matrix A filled with random values. We will then compare the default matrix display to its pretty-formatted counterpart.

A = rand(3,3) % A is now a 3x3 matrix with randomly generated elements between 0 and 1

Default Matrix Display: A = 0.7548 0.6821 0.5198 0.6253 0.9354 0.9111 0.4221 0.8878 0.0912

Pretty Matrix Display: A = 0.755 0.682 0.519 0.625 0.935 0.911 0.422 0.888 0.091

Example 2 - Customizing Matrix Display with Format Specifiers

Now, let’s display matrix A using a custom format specifier that will round the elements up to 2 decimal places:

A_formatted = format(A, ‘@2.2f’) % This returns A formatted according to @2.2f format string (rounding to 2 decimal places)

Displaying A in this manner gives us: A_formatted = 0.75 0.68 0.52 0.63 0.94 0.91 0.42 0.89 0.09

Example 3 - Using Format Functions and Print Functions for Custom Matrix Display

In this example, we will use the format function along with the pretty display to alter matrix A’s format:

A_formatted = pretty(format(A,'@2.2f')) % This returns A formatted by combining pretty display with @2.2f format string

Displaying A in this manner gives us: A_formatted = 0.75 0.68 0.52 0.63 0.94 0.91 0.42 0.89 0.09

Example 4 - Customizing Matrix Display with Display Options and Format Strings

Finally, let’s visualize matrix A using the ‘format compact’ function along with a custom format string:

A_formatted = format(A,'@2.2f',… ‘FormatCompact’,‘on’,… ‘FormatWidth’,5) % This returns A formatted with @2.2f format string, FormatCompact set to true and Formatting width as 5 for each matrix element

Displaying A in this manner gives us: A_formatted = 0.75 0.68 0.52 0.63 0.94 0.91 0.42 0.89 0.09

Conclusion

Matrix display options in MATLAB provide you with multiple ways to view your data in a way that best suits your needs and preferences. With a strong understanding of the various options available, you will be able to create informative and visually appealing representations for your programs and algorithms. By mastering these concepts and implementing them into your work, you can effectively leverage MATLAB’s power as an essential tool in data analysis, linear algebra, and computer vision applications.