필터 지우기
필터 지우기

find index of cell array

조회 수: 5 (최근 30일)
Mohamuud hassan
Mohamuud hassan 2015년 4월 11일
댓글: Stephen23 2015년 5월 27일
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
Mohamuud hassan
Mohamuud hassan 2015년 4월 11일
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.

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

채택된 답변

Stephen23
Stephen23 2015년 4월 11일
편집: Stephen23 2015년 4월 11일
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
Stephen23
Stephen23 2015년 4월 12일
편집: Stephen23 2015년 4월 12일
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 2015년 4월 12일
Thank you Stephen Cobeldick, this step, is best way that i can achieve my goal. again and again thank you for your continued support

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

추가 답변 (1개)

Stephen23
Stephen23 2015년 4월 11일
편집: Stephen23 2015년 4월 11일
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
Stephen23
Stephen23 2015년 4월 11일
편집: Stephen23 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
Mohamuud hassan 2015년 4월 11일
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.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by