find sequence in a matrix
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
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)
If I is a matrix, you will have to tell us your search direction and the role of k.
4 commentaires
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]
Can it find the pattern
1 0 1
1 0 0
?
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)
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].
Plus de réponses (4)
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)
If you want the first location, just take indexes(1).
3 commentaires
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
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
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.
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;
C=conv2(I,[0,1],'valid')./conv2(I,[1,1],'valid')==1
C(:,1:k-1)=0;
[~,loc]=max(C,[],2)
0 commentaires
Voir également
Catégories
En savoir plus sur Geographic Plots 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!