Making part of a column negative depending on another column

1 回表示 (過去 30 日間)
Christopher
Christopher 2013 年 9 月 10 日
Hello,
I am trying to do the following. I have two column vectors as follows:
a=[3;5;8;2;-2;-5;-8];
b=[1;2;3;4;5;6;7];
I am trying to make the values of b corresponding to the locations of a negative in a, negative. so the result would be
b_new=[1;2;3;4;-5;-6;-7]
The last three values of b would be changed to negative because the last three values of a are negative. Both column vectors are the same length.

採用された回答

Jeff E
Jeff E 2013 年 9 月 10 日
b_new = sign(a) .* b

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by