How to count a specific string in a cell array?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Stephen23
el 23 de Mzo. de 2017
>> c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'};
>> nnz(strcmp(c,'water'))
ans =
0
>> nnz(strcmp(c,'car'))
ans =
3
3 comentarios
KSSV
el 23 de Mzo. de 2017
+1
Hadi Ghahremannezhad
el 23 de Mzo. de 2017
BN
el 11 de Abr. de 2020
Categorías
Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!