The cell with the max number of elements

조회 수: 4 (최근 30일)
Maro
Maro 2015년 5월 4일
댓글: Maro 2015년 5월 6일
I have 1x8 cell array and I want t get the cell with the max number of elements for example for the following cell array
I need the answer to be cell number 7 can I do this with function max?

채택된 답변

James Tursa
James Tursa 2015년 5월 4일
>> c = {1 [1,2] [1,3] [1,2,4] [1,2,4,5] [1,6] [1,2,4,5,7] nan}
c =
[1] [1x2 double] [1x2 double] [1x3 double] [1x4 double] [1x2 double] [1x5 double] [NaN]
>> [~,x] = max(cellfun(@numel,c))
x =
7
  댓글 수: 8
Titus Edelhofer
Titus Edelhofer 2015년 5월 5일
Hi Maro,
this is the time I guess where a loop will be easier than trying to do this in a one liner, something like
res = zeros(1, size(c,2));
for i=1:length(res)
val = c(:, i);
% skip NaN
idxNaN = find(~cellfun(@(x) numel(x)==1 && isnan(x), val, 'UniformOutput', true));
% look for min in "allowed cells only"
[~,x] = min(cellfun(@numel,val(idxNaN)));
% be careful with the indices:
res(i) = idxNaN(x);
end
Admittedly not tested ...
Titus
Maro
Maro 2015년 5월 6일
Hi, I have found more efficient solution..
L=cellfun(@numel,c);
L(L== 1) = Inf;
[~,x]=min(L,[],2);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by