How to count a specific string in a cell array?
58 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hadi Ghahremannezhad
am 23 Mär. 2017
Kommentiert: BN
am 11 Apr. 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
0 Kommentare
Akzeptierte Antwort
KSSV
am 23 Mär. 2017
c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'} ;
idx = strfind(c, 'car');
idx = find(not(cellfun('isempty', idx)));
N = length(idx)
2 Kommentare
Weitere Antworten (1)
Stephen23
am 23 Mär. 2017
>> c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'};
>> nnz(strcmp(c,'water'))
ans =
0
>> nnz(strcmp(c,'car'))
ans =
3
3 Kommentare
Siehe auch
Kategorien
Mehr zu Design and Simulate SerDes Systems finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!