hi! I have a problem with matlab: I have a string cell array with variousty size (1024*1) and i must be find one string if it is in cell array and location of it in cell array. any one help me? thank ect
s{1,1}={'01' '02' '0123' '04' '14' '0124' '34' '03' '0134' '23' '24' '0234' '12' '13' '1234'}
s{2,1}= {'012' '024' '3' '034' '023' '1' '123' '013' '4' '014' '134' '2' '234' '124' '0'}
i must find '4' if is appear in s and location of '4' in cell array s.

댓글 수: 1

Stephen23
Stephen23 2015년 3월 12일
편집: Stephen23 2015년 3월 12일
Although this has already been accepted, there is a neater solution using a better data structure of a cell array of strings (rather than cell array of cell arrays), together with strfind. This returns the location indices in a cell array the same size as s:
>> s(2,:)={'012','024','3','034','023','1','123','013','4','014','134'};
>> s(1,:)={'01','02','0123','04','14','0124','34','03','0134','23','24'};
>> strfind(s,'4')
ans =
[] [] [] [2] [2] [4] [2] [] [4] [] [2]
[] [3] [] [3] [] [] [] [] [1] [3] [3]

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 4월 29일

1 개 추천

out = cellfun(@(x)regexp(x,'4'),s,'un',0);
ADDED on Nguyen's answer :)
out = cellfun(@(x)regexp(x,'^4$'),s,'un',0)
or:
out = cellfun(@(x)ismember(x,'4'),s,'un',0)

댓글 수: 3

Nguyen Trung
Nguyen Trung 2012년 4월 30일
thank you very much! but i want to know the location of element which i find in cell array, how can i do?
Image Analyst
Image Analyst 2016년 1월 19일
mhm's "flag" moved here so that it is a comment rather than a flag: Then I removed the flag.
find element in cell
ibrahim Salim
ibrahim Salim 2018년 1월 24일
편집: ibrahim Salim 2018년 1월 24일
Hello, I'd like to ask How can I delete zeros(logical values) from cell 'out'? Thanks

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

추가 답변 (1개)

Richard Brown
Richard Brown 2012년 4월 29일

0 개 추천

Can you define your cell array differently (can it be a 2D cell array, rather than a cell array of cell arrays?) If so, then
s(1,:)={'01' '02' '0123' '04' '14' '0124' '34' '03' '0134' '23' '24' '0234' '12' '13' '1234'}
s(2,:)= {'012' '024' '3' '034' '023' '1' '123' '013' '4' '014' '134' '2' '234' '124' '0'}
[I, J] = find(strcmp(s, '4'))

댓글 수: 4

Nguyen Trung
Nguyen Trung 2012년 4월 30일
you can check your define? If i define follow your advice, matlab had an error:
"Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
And it had no result. Thank any way, any body help me?
Richard Brown
Richard Brown 2012년 4월 30일
Try clear s first
Nguyen Trung
Nguyen Trung 2012년 4월 30일
Ok! thanks! But the cell array i have is generated by my function and i don't know how to save similar to your answer! Can you help me?
Richard Brown
Richard Brown 2012년 4월 30일
OK, so long as each entry of s has the same number of entries (15 in this case), then first run
s = vertcat(s{:})

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2012년 4월 29일

편집:

2018년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by