MATLAB Answers

How to return the smallest vector.

2 views (last 30 days)
jg on 17 Jan 2020
Commented: Matt J on 17 Jan 2020
If I have N vectors of different length how would i isolate and return the vector of the smallest length? I dont care what the length is i just need which is the smallest one.


Sign in to comment.

Accepted Answer

Matt J
Matt J on 17 Jan 2020
Edited: Matt J on 17 Jan 2020
If you "have them" in cell array form, then you could do something like this:
>> vectors={[3 4 5],[1,2],[5 6 7 8]}
vectors =
1×3 cell array
{1×3 double} {1×2 double} {1×4 double}
>> [~,imin]=min(cellfun('length',vectors));
>> smallest=vectors{imin}
smallest =
1 2


jg on 17 Jan 2020
Thank you! I didnt even know cell array existed.
Image Analyst
Image Analyst on 17 Jan 2020
Well, what form were they in? Just separate variables? If so, you need to check their lengths one at a time.
Matt J
Matt J on 17 Jan 2020
Or, you will have to load them into a cell array manually,

Sign in to comment.

More Answers (0)

Sign in to answer this question.