Add value x to negaive numbers and subtract value x from positiv numbers in same double
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi there,
lets say i the double [5 4 3 2 1 -1 -2 -3 -4 -5] and the value x=0.5.
I would like to subtract x from all positiv numbers of the double and add x to all negative numbers. How to go about this ?
Thanks for your time and help
Ben !
댓글 수: 0
채택된 답변
John D'Errico
2023년 7월 11일
편집: John D'Errico
2023년 7월 11일
Trivial. Though you do not say what happens to zero.
V = [5 4 3 2 1 -1 -2 -3 -4 -5];
x = 0.5;
V - x*sign(V)
As I said, It is very important in computing to consider the special cases, as if you don't do that then you will find bugs arise.
sign(0)
So what happens if V has an exactly zero element? Since sign(0) is zero, it leaves the zero alone.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!