find index of cell array
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 11 Avr 2015
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
le 11 Avr 2015
Mohamuud hassan
le 11 Avr 2015
Stephen23
le 27 Mai 2015
This question is continued here (with bug fix!):
Réponse acceptée
Plus de réponses (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 commentaires
Mohamuud hassan
le 11 Avr 2015
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
le 11 Avr 2015
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!