Reputation: 2357
argument = [new_letter_A, new_letter_B, new_letter_C, new_letter_D, new_letter_E];
In Python, I can use len(argument) to find the number of items in the array above. Is there an easy way to accomplish the same thing in MATLAB? (I want this to return '5'.)Thank you.
Upvotes: 0
Views: 230
Reputation: 4686
There are many way to do this such as numel
, length
and size
. MATLAB works with 2d arrays/matrices.
If your matrix is n x m
:
numel
would be n*mlength
would be max(n, m)size
would be n. You can use size(argument, 2) to get m.In the case of 1-d array, they are all the same.
Upvotes: 0
Reputation: 2447
You should either store your vectors as rows and get the size of the first dimension
argument = [new_letter_A; new_letter_B; new_letter_C; new_letter_D; new_letter_E];
size(argument, 1)
Or you can store each vector as a cell in a cell array
argument = {new_letter_A, new_letter_B, new_letter_C, new_letter_D, new_letter_E};
length(argument)
One of the advantages of the second approach is that you can use cellfun
to apply a function to each letter (for example, if you had a function which you were using to compress each letter...)
Upvotes: 3