replacing values in a vector with new value?

15 ビュー (過去 30 日間)
Leslie Paul
Leslie Paul 2016 年 8 月 27 日
コメント済み: Azzi Abdelmalek 2016 年 8 月 27 日
Basically, I have a large vector and want to add 180 to all the negative values and subtract 180 to all positive values in the vector. Finally, I should have the same size vector with the changes. Appreciate a helping hand

回答 (2 件)

James Tursa
James Tursa 2016 年 8 月 27 日
編集済み: James Tursa 2016 年 8 月 27 日
v = your vector
n = v < 0; % the negative indexes (logical)
p = v > 0; % the positive indexes (logical)
v(n) = v(n) + 180;
v(p) = v(p) - 180;
Note: This doesn't change any exact 0 values, per your description.

Image Analyst
Image Analyst 2016 年 8 月 27 日
Try this:
v = v - sign(v) * 180
where v is your vector. It will do what you asked for.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by