How to make a variable negative if another variable is negative?

23 vues (au cours des 30 derniers jours)
Andrew Lackey
Andrew Lackey le 21 Sep 2021
Commenté : Andrew Lackey le 21 Sep 2021
I have two columns (X & Y) full of numbers 200 numbers in a text file.
All values of X are positive, while the values of Y are mixed with negative and positive numbers.
I'm trying to create a simple if statement, so change all of the X values that correspond to a negative Y value, to negative.
This code does not work though:
Code:
if Y < 0
X1 = X*(-1)
end
Unrecognized function or variable 'Y'.

Réponse acceptée

Matt J
Matt J le 21 Sep 2021
X1=X.*sign(Y);

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB 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