Effacer les filtres
Effacer les filtres

how to count more than 1 element in cell array?

2 vues (au cours des 30 derniers jours)
alice
alice le 22 Juin 2012
Hello
I have cell array data example. D = {[1 2 5];[1 2 4 6];[2 4 5 6];[2 5];[1 4 6]}
the answer is 3
I want to know how to calculate a frequency of [2 5] which occurs in D by not using loop.
thank you
  1 commentaire
Walter Roberson
Walter Roberson le 22 Juin 2012
[2 5] must be the only element? 2 and 5 must be adjacent? In that order or order is irrelevant?
Some of these possibilities I don't think can be done without at least an implicit loop using one of the *fun() functions.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 22 Juin 2012
Can so?
out = sum(cellfun(@(x)all(ismember([2 5],x)),D));
  2 commentaires
Ryan G
Ryan G le 22 Juin 2012
Just beat me to it!
alice
alice le 22 Juin 2012
thanks
it's work

Connectez-vous pour commenter.

Plus de réponses (2)

Ryan G
Ryan G le 22 Juin 2012
I think this may be close to what you are looking for.
freqElements = sum(cellfun(@(x) mean(ismember([2 5],x)) == 1,D));

alice
alice le 22 Juin 2012
Oh thank you so much
Both answers are work!
  1 commentaire
Walter Roberson
Walter Roberson le 22 Juin 2012
cellfun() is really a loop.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by