get the amount of rows in an array in matlab

To get the amount of rows in an array in MATLAB, you can use either the size or length function. Here's an example using the size function:

main.m
A = [1 2 3; 4 5 6; 7 8 9];
num_rows = size(A, 1);
50 chars
3 lines

In this example, A is a 3x3 array. The second argument of the size function specifies the dimension you want to query (in this case, the rows). The output of size(A, 1) is 3, which is the number of rows in A.

You can also use the length function, although it behaves in a slightly different way. The length function gives the length of the largest dimension of an array. So if you have a 3x3 array, length will return 3, which is the length of the rows or columns:

main.m
A = [1 2 3; 4 5 6; 7 8 9];
num_rows = length(A);
49 chars
3 lines

In this case, num_rows is also 3, which is the length of the rows in A.

related categories

gistlibby LogSnag