The cell with the max number of elements

4 vues (au cours des 30 derniers jours)
Maro
Maro le 4 Mai 2015
Commenté : Maro le 6 Mai 2015
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?

Réponse acceptée

James Tursa
James Tursa le 4 Mai 2015
>> 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 commentaires
Titus Edelhofer
Titus Edelhofer le 5 Mai 2015
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 le 6 Mai 2015
Hi, I have found more efficient solution..
L=cellfun(@numel,c);
L(L== 1) = Inf;
[~,x]=min(L,[],2);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by