Finding location of change of signs of a matrix's elements

38 vues (au cours des 30 derniers jours)
Volkan Yangin
Volkan Yangin le 18 Fév 2017
Commenté : MA-Winlab le 7 Avr 2019
Hi everbody, I have a matrix like this:
A=[0 0 0 0.33 0.33 0.33 0 0 0 -0.33 -0.33 -0.33 0 0 0];
How can i find location of change of signs? I get an error while using find and diff command. I think, i have made a mistake because of the < and > symbols. May you help me? Thanks.

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Fév 2017
find(diff(sign(A)))+1
to return the locations of the beginning of the new sign.
Note that this includes changing to 0, as 0 has a different sign than positive numbers or negative numbers.
  3 commentaires
MA-Winlab
MA-Winlab le 6 Avr 2019
@Roberson, I tried your approach for finding the locations of change from 0 to 1 and from 1 to 0, but they are in one array.
So, if I have this array and want to find the locations of change from 1 to 0 (returned in an array) and the locations of change from 1 to 0 (returned in a SEPRATE array), how can I modify this line?
Thank you
A=[0 0 0 1 1 1 0 0 0 1 1 1 0 0 0];
MA-Winlab
MA-Winlab le 7 Avr 2019
Update
I figured out how to do that (in my case, the vector of 0s and 1s has an even number):
OpenClose = find(diff(sign(A))) + 1;
OneToZero = OpenClose(1:2:end)
ZeroToOne = OpenClose(2:2:end)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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!

Translated by