find index of cell array
이전 댓글 표시
Hello all, suppose i have cell array 6x1 which contains abstracts of articles, so each row of the cell contains 800 character. if i wan to find the position of str='b e h i n d' from that cell array,only b will be generated, so what is wrong my code. the cell arrays data is in the attachment or myabs.mat
load ('myabs.mat');
mydata=X;
[row,column]=size(mydata);
distance=zeros(row,column);
str='behind using a cover';
j=1;
for n=1:row
gec=char(mydata{n});
%for j=1:length(str)
for m=1:800
while j<length(str)
%for j=1:length(str)
if (gec(m)==str(j))
indexx(j)=m;
distance(n,m)=indexx(j);
end
break;
j=j+1;
end
end
end
댓글 수: 4
Stephen23
2015년 4월 11일
Please edit your question and format the code correctly using the {} Code button that you will find above the textbox.
Also it would be easier if you simply uploaded the .MAT file that you are referring to, for us to try out ourselves.
Mohamuud hassan
2015년 4월 11일
Mohamuud hassan
2015년 4월 11일
Stephen23
2015년 5월 27일
This question is continued here (with bug fix!):
채택된 답변
추가 답변 (1개)
This task could be performed much faster and more robustly by using inbuilt functions. Why not just use strfind instead of these slow and buggy nested loops:
>> A = {'some line of words.','more text to search','some words with other words too','different information here'};
>> B = strfind(A,'words');
>> B{:}
ans =
14
ans =
[]
ans =
6 23
ans =
[]
which returns a cell array of indices giving the locations in each string where the search text 'words' was found. It correctly found the one instance in the first string, and two instances in the third string.
댓글 수: 3
Mohamuud hassan
2015년 4월 11일
It is not clear what the difference is: you want to match every character sequentially, whereas strfind matches the whole string at once. What is the difference in your eyes? Do these characters have to be sequential?
Can you please give an example of one string to be searched and the pattern to be found, and show us how the pattern should be matched to the string.
Mohamuud hassan
2015년 4월 11일
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!