MATLAB Answers

AP

Find the index of the element of a cell array which has the maximum size

Asked by AP
on 4 Jun 2011

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

Tags

Products

No products are associated with this question.

1 Answer

Answer by Jan Simon
on 4 Jun 2011
 Accepted answer
[max_size, max_index] = max(cellfun('size', B, 1))

  0 Comments


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today