find index of cell array

3 visualizaciones (últimos 30 días)
Mohamuud hassan
Mohamuud hassan el 11 de Abr. de 2015
Comentada: Stephen23 el 27 de Mayo de 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
  4 comentarios
Mohamuud hassan
Mohamuud hassan el 11 de Abr. de 2015
simply what i want is. to find str's characters indexces sequentially one by one from the cell array which holds 6 rows of articles abstract and each row contains 800 characters. for example the first character of str is 'b', so we get it in the index 135 according to the first abstracts i mean first row of my cell array, after that we will find the next character of str which is 'e', and it was estimated to be found in index 149 of first row but the result is zero....so what is wrong from my code. that is my question? please help me.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Abr. de 2015
Editada: Stephen23 el 11 de Abr. de 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 comentarios
Stephen23
Stephen23 el 12 de Abr. de 2015
Editada: Stephen23 el 12 de Abr. de 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.
Mohamuud hassan
Mohamuud hassan el 12 de Abr. de 2015
Thank you Stephen Cobeldick, this step, is best way that i can achieve my goal. again and again thank you for your continued support

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 11 de Abr. de 2015
Editada: Stephen23 el 11 de Abr. de 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.
You should avoid calling variables i or j, as these are both names of the inbuilt imaginary unit.
  3 comentarios
Stephen23
Stephen23 el 11 de Abr. de 2015
Editada: Stephen23 el 11 de Abr. de 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
Mohamuud hassan el 11 de Abr. de 2015
simply what i want is. to find str's characters indexces sequentially one by one from the cell array which holds 6 rows of articles abstract and each row contains 800 characters. for example the first character of str is 'b', so we get it in the index 135 according to the first abstracts i mean first row of my cell array, after that we will find the next character of str which is 'e', and it was estimated to be found in index 149 of first row but the result is zero....so what is wrong from my code. that is my question? please help me.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by