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일

1 개 추천

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

댓글 수: 2

Thanks a lot. It works very fine.
KSSV
KSSV 2017년 3월 23일
@ Hadi Ghahremannezhad Stephen Cobeldick's answer is perfect one...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2017년 3월 23일

10 개 추천

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

댓글 수: 3

KSSV
KSSV 2017년 3월 23일
+1
@KSSV This works great and simple. thanks
BN
BN 2020년 4월 11일
Thanks from 2020, It's Amazing approach, Thank You Dear Stephen Cobeldick. +1

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

2017년 3월 23일

댓글:

BN
2020년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by