replacing values in a vector with new value?
13 次查看(过去 30 天)
显示 更早的评论
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
1 个评论
Azzi Abdelmalek
2016-8-27
This is almost the same question you posted here http://www.mathworks.com/matlabcentral/answers/300057-i-want-add-a-value-to-every-element-that-is-less-than-zero-in-my-vector#comment_386034
回答(2 个)
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.
0 个评论
Image Analyst
2016-8-27
Try this:
v = v - sign(v) * 180
where v is your vector. It will do what you asked for.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!