How to Get the Number of Columns of a Matrix in MATLAB

Mehak Mubarik Feb 02, 2024
  1. Use the size() Function to Get the Number of Columns of a Matrix in MATLAB
  2. Use the length() Function to Get the Number of Columns of a Matrix in MATLAB
  3. Access the end Keyword to Get the Number of Columns of a Matrix in MATLAB
  4. Conclusion
How to Get the Number of Columns of a Matrix in MATLAB

MATLAB, a powerful numerical computing environment, is widely used for various applications in engineering, science, and mathematics. One common task when working with matrices is determining the number of columns they possess.

MATLAB allows a user to manipulate any matrix using the matrix-algebra method to easily calculate difficult and long formulas. A matrix, in MATLAB, is a rectangular array that contains any data we enter and organize according to our requirements.

The vertical entries of our data fill the columns of a matrix, whereas the horizontal data entries in our matrix are known as rows. We will look at different functions and calculate the number of columns of a matrix with the help of MATLAB.

Use the size() Function to Get the Number of Columns of a Matrix in MATLAB

The size() function is a versatile tool in MATLAB that provides essential information about the dimensions of an array, which includes matrices.

It returns a vector of positive integers, where each element corresponds to the size of a particular dimension. For a 2D matrix, the first element indicates the number of rows, and the second element denotes the number of columns.

Syntax:

[d1, d2, ..., dn] = size(X);

Here’s what each part of the syntax represents:

  • d1, d2, ..., dn: These are variables that will hold the sizes of the corresponding dimensions of the array X.
  • X: This is the array or matrix whose dimensions you want to retrieve.
  • size(): This is the MATLAB function that returns the sizes of the dimensions of X.
  • n: This indicates the number of dimensions of the array X.

If X is a 2D matrix, n will be 2. In this case, d1 will represent the number of rows, and d2 will represent the number of columns. If X is a multidimensional array, there will be more variables to represent the sizes of each dimension.

The size() function supports two arguments. The first argument is the name of our matrix (with some data), of which we want to get the number of columns.

The second argument can be either 1 or 2. The numeric number 1 returns the number of rows of the matrix, whereas the numeric number 2 returns the number of columns.

According to our requirements, we will use the numeric number 2 to get the number of matrix columns in MATLAB.

Let us understand this concept by looking at the following example:

%Suppose our matrix is:
matrix = [12 22 32; 42 52 62; 72 82 92];
size(matrix,2)

We have defined a 3x3 matrix called matrix. This matrix contains numerical values arranged in a 3x3 grid.

We used the size() function with the second argument set to 2. This means you are querying the size of the matrix along its second dimension, which corresponds to the number of columns.

Output:

ans = 3

Note that the output returned with the number of columns of the matrix named matrix in the above example, as we used the numeric number 2.

Let’s use the size() function but in a different way.

%Suppose our matrix is:
matrix = [12 22 32; 42 52 62; 72 82 92];
[row,column] = size(matrix); %get-row-and-column-values-of-data-matrix
fprintf('\nNumber of rows of our matrix is: %d' ,row); %print-number-of-row
fprintf('\nNumber of columns of our matrix is: %d ' ,column); %print-number-of-column

Note that we did not pass any numeric argument in this example. Use the [row,column] = size(matrix); to directly obtain the number of columns and rows of our matrix from this code through the size() function.

Output:

Number of rows of our matrix is: 3
Number of columns of our matrix is: 3

Use the length() Function to Get the Number of Columns of a Matrix in MATLAB

The length() function in MATLAB is primarily designed to determine the length of vectors or arrays along a specified dimension. For matrices, it is most commonly used to obtain the number of columns.

Syntax:

L = length(A)
  • L: The output variable that will store the length.
  • A: The input matrix.

The length() function in MATLAB is used to return the value of a non-zero, not empty, matrix.

The argument to be passed is the name of our matrix that contains our data. The function length(name-of-our-matrix) returns the largest number, whether row or column.

%Suppose our matrix is:
matrix = [12 22 32; 42 52 62; 72 82 92];
length(matrix)

Output:

ans = 3

Note that we got the same answer for the length() function as we got for the size() function. The reason is that our matrix is square.

That is why we got the same answer because the number of rows of our matrix is equal to the number of columns of our matrix.

While using the length() function for column determination is powerful, it’s crucial to remember that it specifically targets the second dimension of a matrix. If you require the number of rows, other methods like size() should be employed.

Access the end Keyword to Get the Number of Columns of a Matrix in MATLAB

The end keyword is a fundamental component of MATLAB’s indexing system. It provides a convenient way to reference the last element along a specific dimension of an array.

This keyword is particularly useful when working with multidimensional arrays, allowing you to access elements without knowing the exact size of the array.

The syntax for using end varies depending on the context:

  1. Accessing Elements

    To access the last element along a specific dimension, you can use end in indexing expressions. The syntax is:

    A(end)
    

    This will refer to the last element of the array A.

  2. Specifying a Range

    You can use end in conjunction with a starting index to specify a range. The syntax is:

    A(starting_index:end)
    

    This will create a range starting from starting_index up to the last element.

  3. Multidimensional Arrays

    In the context of multidimensional arrays, you can use end along with colons : to access entire rows or columns. The syntax is:

    A(:, end) % Accessing the last column
    A(end, :) % Accessing the last row
    

    This is useful for tasks like accessing the last column or row of a matrix.

  4. Defining Array Size

    When creating an array, end can be used to define its size. For example:

    A = zeros(1, end); % Creates a row vector with size determined by `end`
    

    In this case, end represents the size of the array.

Since MATLAB’s end keyword can be used to refer to the last element along a particular dimension. This can be applied to find the number of columns.

matrix = [12 22 32; 42 52 62; 72 82 92]; % Creating a sample matrix
lastColumn = matrix(:, end); % Accessing the last column using the end keyword
numCols = length(lastColumn); % Obtaining the number of columns
disp(['Number of Columns: ' num2str(numCols)]);

This code snippet creates a sample 3x3 matrix named matrix, extracts its last column using the end keyword, calculates the number of columns (which is the number of rows in the original matrix), and then displays this information on the command window.

If you run this code in MATLAB, you’ll see the output:

Number of Columns: 3

Conclusion

In conclusion, this tutorial has explored various methods to determine the number of columns in a matrix using MATLAB.

We started by utilizing the versatile size() function, which provides essential information about the dimensions of an array, including matrices. Next, we employed the length() function to obtain the number of columns, understanding that it primarily targets the second dimension of a matrix.

Furthermore, we delved into the powerful end keyword, a fundamental component of MATLAB’s indexing system. This keyword allows for convenient access to the last element along a specified dimension, providing a valuable tool when working with multidimensional arrays.

Mehak Mubarik avatar Mehak Mubarik avatar

Mehak is an electrical engineer, a technical content writer, a team collaborator and a digital marketing enthusiast. She loves sketching and playing table tennis. Nature is what attracts her the most.

LinkedIn

Related Article - MATLAB Matrix