필터 지우기
필터 지우기

count string in a cell array

조회 수: 4 (최근 30일)
Collegue
Collegue 2019년 11월 21일
편집: Ridwan Alam 2019년 11월 22일
How can I count how many times a pattern of a string /char is presented in a cell array ????
string = 945
cell = {1234, 9456, 9457 }
I want the result to be = 2
  댓글 수: 1
the cyclist
the cyclist 2019년 11월 21일
편집: the cyclist 2019년 11월 21일
Are the contents of the cell array numeric (e.g. class "double"), string, or character array?
Similarly, is the variable you named "string" a numeric, a string, or a character array. (Calling a variable "string" or "cell" is a bad idea, because those are MATLAB keywords.)
If one of your cell elements was 9457945, would that count once, or twice, in the total?

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

답변 (2개)

Ridwan Alam
Ridwan Alam 2019년 11월 21일
cell = {1234, 9456, 9457 }
strong = 945
count = sum(contains(string(cell),string(strong)))
  댓글 수: 3
Ridwan Alam
Ridwan Alam 2019년 11월 21일
Haha, for sure!
Ridwan Alam
Ridwan Alam 2019년 11월 22일
편집: Ridwan Alam 2019년 11월 22일
Dear Collegue,
If you find this solution useful, please accept the answer.
If there is something wrong, please let me know as well.
Thanks!!

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


Stephen23
Stephen23 2019년 11월 21일
>> S = '945';
>> C = {'1234', '9456', '9457'};
>> nnz(~cellfun(@isempty,strfind(C,S)))
ans = 2

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by