Find the index of the element of a cell array which has the maximum size
2 views (last 30 days)
Show older comments
I have a cell array (B) which has elements having two columns and different number of rows. I want to find the element which has the largest number of rows. I wrote the following code which seems to me non-professional. Is there a better way to do that?
max_index=0;
max_size=0;
for i=1:numel(B)
if max_size<size(B{i},1)
max_size=size(B{i},1);
max_index=i;
end
end
Thanks.
0 Comments
Accepted Answer
More Answers (1)
Jos (10584)
on 26 Oct 2017
NrowsB = cellfun('size',B,1) ;
[~, ri] = sort(NrowsB)
ri(k) % index of B with the k-th most number of rows
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!