find length of columns in an array in matlab

In Matlab, you can use the size function to get the dimensions (or length) of an array. To find the size of each column in a 2D array, you can use the size function with the second argument set to 1. Here's an example:

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

This will give the output:

main.m
3
2 chars
2 lines

Which tells you that there are 3 columns in the array A.

Similarly, you can also use the length function to get the number of elements in each column. Here's an example:

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

This will give the output:

main.m
3
2 chars
2 lines

Again, telling you that there are 3 columns in the array A. But note that this is not the length of each column. To get the length of each column using the length function, you would need to loop through each column and calculate it separately.

gistlibby LogSnag