# Unravel the Mystery of Plotting Matrices in Matlab

This comprehensive guide will teach you everything about plotting matrices in Matlab, an indispensable aspect of computer vision and data analysis. With the guidance of this tutorial and through code …

Updated November 4, 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 comprehensive guide will teach you everything about plotting matrices in Matlab, an indispensable aspect of computer vision and data analysis. With the guidance of this tutorial and through code samples, we will delve deep into techniques to effectively visualize your datasets for insightful decision-making and problem solving.

Introduction

Plotting matrices in Matlab is a crucial skill when working with computer vision algorithms and data analysis. With the ability to render visually appealing plots, you can gain valuable insights from your data sets, which will greatly improve your understanding of the underlying patterns, trends, and relationships between variables. In this article, we’ll cover everything from the fundamentals of matrices and Matlab plotting to advanced topics such as customizing plot properties, incorporating multiple datasets, and leveraging heatmaps.

Prerequisites

To benefit most from this tutorial, a basic understanding of the following will be helpful:

- Understanding of matrices (rows x columns data structures)
- Familiarity with Matlab’s programming language and syntax
- Working knowledge of fundamental plotting functions in Matlab, such as ‘plot’ and ‘plot2d’
- A good grasp on various ways to import data sets into Matlab, like using MAT-files, CSV files or image data (to visualize them as matrices)
- Knowledge about common Matlab libraries for computer vision tasks, specifically the Image Processing Toolbox and Computer Vision System Toolbox
- Some familiarity with the Data Visualization Toolbox that provides powerful tools to render data in various graphical forms including 3D plotting

Getting Started - The Basics of Matrix Plots in Matlab

Before we delve into advanced topics, let’s begin with a quick overview on how to create and visualize matrices using basic plotting functions in Matlab.

Step 1: Create a matrix Create your data matrix by generating random or imported values (e.g., via image processing algorithms). Here is an example of creating a random matrix of size 2x3 using Matlab’s ‘rand’ function:

A = rand(2, 3)

Step 2: Plotting the Matrix - Simple Approaches Now that we have our data matrix (A), let’s visualize it by plotting each row separately with the use of the ‘plot’ and ‘plot2d’ functions. To do this, iterate over the rows in A and use these functions to generate a line plot for each row:

for i = 1 : size(A, 1) plot(A(i, :)) end

or

for i = 1 : size(A, 1) plot2d(A(i, :), ‘marker’, ‘.') end

These basic plots will show your row data in the form of line graphs or scatter plots respectively. Although this is a straightforward approach to visualizing matrices, it may not be ideal when working with larger datasets as it can consume significant memory space and processing time.

Step 3: Efficient Plotting Techniques For more efficient ways to plot matrices in Matlab, we will leverage matrix operations and specific functions that facilitate visualization without requiring the explicit iteration through rows. Consider the following example:

B = reshape(A, size(A, 1), product(size(A))); % Reshapes A into columns

plot(B)

Here, ‘reshape’ reorganizes our matrix in a more memory-efficient manner and preserves its structure. It converts the original 2x3 array (A) into a vector B with all rows appended together as columns. Then we plot this transformed B matrix to visualize the data contained within A.

Step 4: Handling Complex Matrix Visualization In many real-world scenarios, you will work with more complex matrices and may need to incorporate additional layers of visualization such as heatmaps or contour plots. The Data Visualization Toolbox in Matlab provides useful functions for generating these advanced visual representations.

Consider the following example:

C = rand(5, 6); % Create a matrix C with random values imageplot(reshape(C, [prod(size(C)) 1]), ‘EdgeColor’, ‘none’) % Converts C to a column vector and plots it as an image using heatmap colors

In this example, we transform the original 5x6 matrix (C) into a 30x1 column vector by reshaping and then plot it using the ‘imageplot’ function. This approach converts the data into an image-like representation, with each row representing a column in the original matrix C.

Customizing Your Matrix Plots in Matlab

In order to gain even more insights from your matrices and customize their visual representation according to your specific needs, you may want to manipulate plot properties like axis limits, labels, titles, and other aspects of your graphical output.

Here are some important functions for modifying your Matlab matrix plots:

- ‘axis’: This function is used to set various properties of the plot’s axes (x-axis, y-axis, z-axis limits, etc.)
- ‘title’: Add a title to your plot with the appropriate string as an argument
- ‘xlabel’, ‘ylabel’, ‘zlabel’: Label each axis for better understanding and readability
- ‘legend’: Generate a legend with explanations of data series for more detailed visualization
- ‘grid on/off’: Display or remove grid lines for the plot to improve visibility
- ‘hold on/off’: If you have multiple plots to display, use this command to ensure they are drawn in the same graph
- ‘colorbar’: Add a color bar next to your heatmap, which is useful when working with matrices consisting of continuous values (e.g., temperature maps)
- ‘hilbert’: Generate Hilbert curves for visualizing high-dimensional data. This can be useful when working with tensor or array representations.

These functions provide flexibility in adjusting various aspects of your plot to suit your needs and better analyze the patterns within your datasets.

Plotting Multiple Datasets with Matlab - Overlaying Matrix Plots

In many real-world scenarios, you might have multiple matrices (i.e., different datasets) that need visualization simultaneously for a comprehensive analysis. The following steps will guide you on plotting these multiple datasets in a single graph using Matlab:

- Create separate matrices representing each dataset - Let’s assume you have two matrices: A and B.
- Combine the data matrices into one matrix - This can be done by concatenating rows together. For example, for A and B: C = [A; B]
- Reshape the combined matrix (C) to form a vector similar to that of Step 4 in “Handling Complex Matrix Visualization” section. Let’s assume this transformation results in a 20x1 vector D.
- Plot this new vector using either ‘plot’ or more advanced plotting functions like ‘imageplot’, ‘contourplot’, etc., according to your needs.

By following these steps, you can overlay multiple datasets representing matrices on a single graph and gain a more comprehensive understanding of their relationships and underlying patterns.

Conclusion

Visualizing matrix data in Matlab is an essential skill for effective data analysis and computer vision applications. In this article, we covered the fundamentals of plotting matrices with basic Matlab functions, efficient approaches to memory handling, customization options, visualizing multiple datasets, and advanced matrix representations like heatmaps or contour plots. With these skills under your belt, you will be well-equipped to tackle a wide range of computer vision problems and gain valuable insights from your data sets.