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

24 ビュー (過去 30 日間)
Andrew Lackey
Andrew Lackey 2021 年 9 月 21 日
コメント済み: Andrew Lackey 2021 年 9 月 21 日
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'.

採用された回答

Matt J
Matt J 2021 年 9 月 21 日
X1=X.*sign(Y);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by