If you need to deal with matrices that have lots of ’0’ values in their elements, you can use ‘sparse()’ code in Matlab® to define and illustrate them. In this article, we will show you how to use the ‘sparse()’ command in Matlab® to define and illustrate matrices that have lots of ‘zeros’ inside them as elements.
For example, there is a matrix such as ‘a’ as shown by the red arrow that includes lots of zeros inside it. And the elements that are non-zero integers are in minority or ‘sparse’ in that matrix. We can show these kinds of matrices in the form of ‘b’ as shown by the second green arrow above by typing sparse(a) code as shown by the green arrow again.
Also, you can create matrices directly from the sparse() command in Matlab®. As we stated above the use of the sparse() command is very helpful for matrices that include zeros in the majority of their elements. So you can only define the minority of non-zero integers inside these matrices, and the places of them.
YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn MatLab®!
For example, we created three vectors inside the green box above that the first one includes the row numbers of minority non-zero integers, the second one include the column number of minority non-zero integers. And the values of these minority integers are given at the third vector. What you need to do is, define these vectors respectively inside the parentheses of sparse() command as shown by the blue arrow above. And you need to add the dimensions of the matrix such as 4, 4 which is a 4×4 matrix.
The illustration of these sparse matrices in the command window is as shown by blue and green arrows. The first column of binary numbers are showing the places of these non-zero numbers inside the matrix, and the second column of integers are the minority non-zero numbers inside the matrix.
The use of sparse() command in Matlab® is very simple like above. Leave your comments and questions about ‘sparse()’ command in Matlab® below!
This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®
Your precious feedbacks are very important to us.