the first time there is zero before and after a particular index
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Masih Alavy
le 30 Juil 2017
Réponse apportée : Image Analyst
le 30 Juil 2017
Hi All, I have a matrix which looks like this:
1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 Let's say that I want to find out at what indices there is the first zero happening before and after index 20. How do we do this? So, in my example here, my points of interest will be: 7 (for before) and 37 (for after).
Thanks
0 commentaires
Réponse acceptée
the cyclist
le 30 Juil 2017
Here is one straightforward way:
x = [1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0];
anchor = 20;
positionIndex = 1:numel(x);
lastPriorZero = max(find(x==0 & positionIndex<anchor))
firstLaterZero = min(find(x==0 & positionIndex>anchor))
0 commentaires
Plus de réponses (1)
Image Analyst
le 30 Juil 2017
If you want to find the 0's just outside the 1's, and you have the Image Processing Toolbox, you can do this:
x = [1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
dilatedx = imdilate(x, [1,1,1]) % Grow 1 regions outward
indexes = find(dilatedx ~= x) % Find locations of mismatches.
and you get
indexes =
4 7 37
as you should.
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!