
Number of rows and columns of a matrix in Matlab
In this lesson, I will teach you how to extract the number of rows and columns of a matrix in Matlab.
Let's take a practical example.
Create a rectangular matrix with two rows and three columns.
>> M=[1 2 3;4 5 6]
M =
1 2 3
4 5 6
To extract the number of rows and columns of the matrix, you need to use the function size()
>> size(M)
The size() function returns the dimensions and the number of rows and columns of the matrix.
The result is a list of values.
ans = 2 3
In this case, the matrix is defined in two dimensions because the list has two values.
- The first value in the list (2) indicates the number of rows of the matrix.
- The second value in the list (3) indicates the number of columns of the matrix.
Therefore, the matrix has 2 rows and 3 columns.
$$ M = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$
It is a rectangular matrix.