How to count a specific string in a cell array?

48 vues (au cours des 30 derniers jours)
Hadi Ghahremannezhad
Hadi Ghahremannezhad le 23 Mar 2017
Commenté : BN le 11 Avr 2020
I'm trying to count the number of occurrences of a word in a cell array. For example if the cell is like this:
c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'}
I want to count the number of occurrences of strings in the cell. For example if I search for the number of string 'water' it would result in *0*. Or if I search for the number of 'car', it would result in *3*. How can I do this?
Thanks in advance

Réponse acceptée

KSSV
KSSV le 23 Mar 2017
c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'} ;
idx = strfind(c, 'car');
idx = find(not(cellfun('isempty', idx)));
N = length(idx)
  2 commentaires
Hadi Ghahremannezhad
Hadi Ghahremannezhad le 23 Mar 2017
Thanks a lot. It works very fine.
KSSV
KSSV le 23 Mar 2017
@ Hadi Ghahremannezhad Stephen Cobeldick's answer is perfect one...

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 23 Mar 2017
>> c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'};
>> nnz(strcmp(c,'water'))
ans =
0
>> nnz(strcmp(c,'car'))
ans =
3
  3 commentaires
Hadi Ghahremannezhad
Hadi Ghahremannezhad le 23 Mar 2017
@KSSV This works great and simple. thanks
BN
BN le 11 Avr 2020
Thanks from 2020, It's Amazing approach, Thank You Dear Stephen Cobeldick. +1

Connectez-vous pour commenter.

Catégories

En savoir plus sur Design and Simulate SerDes Systems dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by