find sequence in a matrix

6 vues (au cours des 30 derniers jours)
shamal
shamal le 11 Mai 2025
Modifié(e) : Torsten le 13 Mai 2025
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]
I = 1×18
0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
saved = 7

Réponse acceptée

Torsten
Torsten le 11 Mai 2025
Modifié(e) : Torsten le 11 Mai 2025
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)
saved = 7
If I is a matrix, you will have to tell us your search direction and the role of k.
  4 commentaires
Image Analyst
Image Analyst le 13 Mai 2025
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]
I = 3×18
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
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Can it find the pattern
1 0 1
1 0 0
?
Torsten
Torsten le 13 Mai 2025
Modifié(e) : Torsten le 13 Mai 2025
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)
ans = 0
diff([1 1 1],2)
ans = 0
diff([1 0 0],2)
ans = 1
diff([0 1 0],2)
ans = -2
diff([0 0 1],2)
ans = 1
diff([1 1 0],2)
ans = -1
diff([0 1 1],2)
ans = -1
diff([1 0 1],2)
ans = 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].

Connectez-vous pour commenter.

Plus de réponses (4)

Image Analyst
Image Analyst le 11 Mai 2025
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)
indexes = 1×5
2 6 12 15 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If you want the first location, just take indexes(1).
If you have a 2-D array, you can use normxcorr2. See attached demo.
  3 commentaires
Image Analyst
Image Analyst le 12 Mai 2025
Modifié(e) : Image Analyst le 12 Mai 2025
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
indexes = 1×4
7 13 16 18
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
indexes = 1×4
6 12 15 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Either way, it's still simpler than the other solutions.
Matt J
Matt J le 12 Mai 2025
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.

Connectez-vous pour commenter.


Matt J
Matt J le 11 Mai 2025
Modifié(e) : Matt J le 13 Mai 2025
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]
I = 3×18
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
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
k=3;
[minval,loc]=min(diff(I(:,k:end),1,2),[],2);
loc(minval>-1)=nan;
loc=loc+(k-1)
loc = 3×1
6 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Matt J
Matt J le 12 Mai 2025
Modifié(e) : Matt J le 13 Mai 2025
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;
Ik=string(char(I(:,k:end)+'0'));
loc=strlength(extractBefore( Ik , '10'))+k
loc = 3×1
6 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Matt J
Matt J le 13 Mai 2025
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]
I = 3×18
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
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
k=3;
C=conv2(I,[0,1],'valid')./conv2(I,[1,1],'valid')==1
C = 3x18 logical array
0 1 0 0 0 1 0 0 0 0 0 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 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0
C(:,1:k-1)=0;
[~,loc]=max(C,[],2)
loc = 3×1
6 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Catégories

En savoir plus sur Geographic Plots dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by