How to find multliple values in larger arrays where the value two below is the opposite sign (+ve/-ve)

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

Réponses (2)

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))];
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

Question posée :

le 15 Nov 2014

Modifié(e) :

le 15 Nov 2014

Community Treasure Hunt

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

Start Hunting!

Translated by