find index of cell array
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohamuud hassan
le 11 Avr 2015
Commenté : Stephen23
le 27 Mai 2015
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
Réponse acceptée
Stephen23
le 11 Avr 2015
Modifié(e) : Stephen23
le 11 Avr 2015
There might be simpler ways to achieve this task, but this seems to work:
str = 'behind';
load myabs
for m = 1:numel(X)
Y = bsxfun(@eq,X{m}(:),str);
for n = 2:size(Y,2)
Y(:,n) = Y(:,n) & cumsum(Y(:,n-1))>0;
end
Z = find(any(diff([false(size(str));cumsum(Y)>0],1),2))
end
and when run it prints this in the command window:
Z =
135
149
235
236
239
266
Z =
72
73
127
136
160
174
Z =
163
170
172
173
177
221
Z =
40
53
65
66
76
112
Z =
216
217
267
279
295
319
Z =
170
175
178
196
197
234
3 commentaires
Stephen23
le 12 Avr 2015
Modifié(e) : Stephen23
le 12 Avr 2015
To store the value of Z just preallocate the output array before the loop and us indexing to allocate the values on each loop iteration:
str = 'behind';
load myabs
Z = nan(size(X,1),numel(str));
for m = 1:numel(X)
Y = bsxfun(@eq,X{m}(:),str);
for n = 2:size(Y,2)
Y(:,n) = Y(:,n) & cumsum(Y(:,n-1))>0;
end
tmp = find(any(diff([false(size(str));cumsum(Y)>0],1),2));
Z(m,1:numel(tmp)) = tmp;
end
and then each row of Z corresponds to one cell of X:
>> Z
Z =
135 149 235 236 239 266
72 73 127 136 160 174
163 170 172 173 177 221
40 53 65 66 76 112
216 217 267 279 295 319
170 175 178 196 197 234
Or you might like to consider using my third solution using regexp, which detects all instances of the pattern string, not just the first one.
Plus de réponses (1)
Stephen23
le 11 Avr 2015
Modifié(e) : Stephen23
le 11 Avr 2015
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
Stephen23
le 11 Avr 2015
Modifié(e) : Stephen23
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.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!