Remove negative numbers
조회 수: 26 (최근 30일)
이전 댓글 표시
Hi all!
So I've been using MatLab for all of about two days and I already need some help. I have a matrix which contains both positive and negative numbers and I want to remove the negative numbers by adding the absolute value of the most negative number to all of the numbers in the matrix. I don't know the value of the most negative number beforehand, so I can't just hardcode it.
ie. Matrix A becomes Matrix B: A = [4 0 -6 -3], B = [10 6 0 3]
Is there an easy one-step solution for this? Or do I need to find the min value first, get the absolute value and then add it to all the matrix values?
Thank you!
댓글 수: 0
채택된 답변
Walter Roberson
2012년 1월 19일
B = A - min(0, min(A(:)));
This code protects against the possibility that none of the entries in A are negative, leaving all the values unchanged in that situation.
댓글 수: 0
추가 답변 (1개)
the cyclist
2012년 1월 19일
If you are sure there is at least one negative number in A, this will work:
B = A - min(A);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!