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!