How to count a specific string in a cell array?

53 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2017-3-23
c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'} ;
idx = strfind(c, 'car');
idx = find(not(cellfun('isempty', idx)));
N = length(idx)

更多回答(1 个)

Stephen23
Stephen23 2017-3-23
>> c = {'car' , 'tree' , 'car' , 'bag' , 'horse' , 'car' , 'tree'};
>> nnz(strcmp(c,'water'))
ans =
0
>> nnz(strcmp(c,'car'))
ans =
3

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by