How to find multliple values in larger arrays where the value two below is the opposite sign (+ve/-ve)
Afficher commentaires plus anciens
I work with larger arrays (100x100) and am trying to develop a script which finds values where the value 2 below is the opposite sign. For example in the array below it would be the '40' and '-4' value.
10 10 10 10 10 10
10 10 40 10 10 10
10 10 10 10 10 10
10 10 -2 10 10 10
10 10 10 10 10 -4
How do I display these values as a correct/incorrect matrix i.e. if a 1 is shown, this is where there is a value with the value two below is the opposite sign. For our above matrix it would be...
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 1
Thanks for any answers
1 commentaire
Azzi Abdelmalek
le 15 Nov 2014
Why 40 and -4?
Réponses (2)
Guillaume
le 15 Nov 2014
Assuming you made a mistake about your -4. Use sign to get the sign of the matrix, and compare them with an offset of 2 on the rows:
m = [10 10 10 10 10 10
10 10 40 10 10 10
10 10 10 10 10 10
10 10 -2 10 10 10
10 10 10 10 10 -4];
s = sign(m);
o = [s(1:end-2, :) ~= s(3:end, :); zeros(2, size(m, 2))];
Star Strider
le 15 Nov 2014
Modifié(e) : Star Strider
le 15 Nov 2014
The (40, -4) isn’t consistent because there is nothing ‘below’ -4 unless you wrap it.
The ‘R’ matrix here gives you a logical matrix indexing the values that are 2 rows above the negative numbers:
A = [10 10 10 10 10 10
10 10 40 10 10 10
10 10 10 10 10 10
10 10 -2 10 10 10
10 10 10 10 10 -4];
R = circshift(A<0,[-2 0]);
producing:
R =
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0
Catégories
En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!