find sequence in a matrix
조회 수: 12 (최근 30일)
이전 댓글 표시
Hi, it possibile to velocize it and avoid loop? (I will be a matrix .,not a single array)
(if is possible to use vectorization)
I=[0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0]
k=3;
for i=1:numel(I)
if i>=k+1
if I(i-1) && ~I(i) %%i want to find the first "1 0"
saved=i;
break;
end
end
end
saved
댓글 수: 0
채택된 답변
Torsten
2025년 5월 11일
편집: Torsten
2025년 5월 11일
I = [0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0];
k = 3;
saved = k + find(diff(I(k:end))==-1,1)
If I is a matrix, you will have to tell us your search direction and the role of k.
댓글 수: 4
Image Analyst
2025년 5월 13일
Where is the search pattern defined in that code? Where does it say it's looking for the pattern [1, 0]?
Also can it find a 2-D pattern in a 2-D matrix. Like for the given matrix
I = [0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0;0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 1;1 1 0 1 0 1 0 0 0 1 0 1 0 0 1 1 1 0]
Can it find the pattern
1 0 1
1 0 0
?
Torsten
2025년 5월 13일
편집: Torsten
2025년 5월 13일
Where is the search pattern defined in that code? Where does it say it's looking for the pattern [1, 0]?
Let x be a vector consisting only of 0 and 1 and assume that the pattern [1 0] somewhere appears in the vector x (if it's not present in x, the method will give a wrong answer).
diff(x) can give values -1, 0 and 1 - so -1 is the minimum value you can get when you compute diff(x). On the other hand, getting -1 for diff(x) in position i means x(i+1) - x(i) = -1 which is only possible if x(i+1) = 0 and x(i) = 1. Thus a position i where diff(x) attains its minimum (namely -1) is a position where the pattern [1 0] (x(i) = 1, x(i+1) = 0) appears in the vector x.
Can it find the pattern
1 0 1
1 0 0
?
diff([0 0 0],2)
diff([1 1 1],2)
diff([1 0 0],2)
diff([0 1 0],2)
diff([0 0 1],2)
diff([1 1 0],2)
diff([0 1 1],2)
diff([1 0 1],2)
Only pattern [0 1 0] gives answer -2 and pattern [1 0 1] gives answer 2. Thus these two patterns could be found by a similar method (min and max) as the one used for pattern [1 0].
추가 답변 (4개)
Image Analyst
2025년 5월 11일
The first [1, 0] shows up at index 2, not 7. It also appears at index 6 and others.
Probably the simplest way (a single line of code) is to use strfind. (Yes it works with numbers as well as character strings).
% Set up parameters:
I=[0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0];
searchPattern = [1, 0];
% Now find all locations where the vector has values [1, 0]
indexes = strfind(I, searchPattern)
If you want the first location, just take indexes(1).
댓글 수: 3
Image Analyst
2025년 5월 12일
편집: Image Analyst
2025년 5월 12일
You didn't explicitly say that. And by looking at your code, it only starts looking at values for i>=k+1, or 4, not 3. And then it looks like it finds the trailing 0, not the leading 1, so it finds index 7 rather than 6 which is where the [1,0] pattern starts. So it was kind of confusing to me. Not sure what you want exactly.
If you want to find the index of the trailing 0 after index 3, you can do
% Set up parameters:
I=[0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0];
k=3;
searchPattern = [1, 0];
% Now find locations where the array has values [1, 0]
indexes = strfind(I(k:end), searchPattern) + k
If you want to find the index of the leading 1 after index 3, you can do
% Set up parameters:
I=[0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0];
k=3;
searchPattern = [1, 0];
% Now find locations where the array has values [1, 0]
indexes = strfind(I(k:end), searchPattern) + k - 1
Either way, it's still simpler than the other solutions.
Matt J
2025년 5월 12일
Simpler, but not as efficient. strfind() will not operate row-wise on a matrix. You will have to loop. Additionally, it will search the entire row, unlike the other solutions which will stop at the first occurence.
Matt J
2025년 5월 13일
I=[0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0;
0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 1;
1 1 0 1 0 1 0 0 0 1 0 1 0 0 1 1 1 0]
k=3;
C=conv2(I,[0,1],'valid')./conv2(I,[1,1],'valid')==1
C(:,1:k-1)=0;
[~,loc]=max(C,[],2)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Geographic Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!