Replace values within a range to zero
조회 수: 146 (최근 30일)
이전 댓글 표시
Hi,
I have an array 26X106 that contains both positive and negative values.
I want to be able to replace the numbers within a specified range to zero - e.g., all numbers >0.25 and <0.25 are replaced by zero.
Any help would be greatly appreciated,
Thanks!
댓글 수: 2
Walter Roberson
2022년 7월 28일
You only want to keep values that are exactly 0.25? exactly equal is the only value that is not less than and not greater than.
답변 (1개)
sudobash
2022년 7월 28일
편집: sudobash
2022년 7월 28일
Hi!
This is how to select numbers from a matrix in a given range.
matr = rand(5)
mask = matr < 0.25 & matr > -0.25
Selecting in this manner returns a logical matrix commonly known as a mask matrix. You can then set the values at those locations to 0 like this.
matr(mask) = 0
Hope this answers your question.
댓글 수: 1
Steven Lord
2022년 7월 28일
You can also do this using the or operator | instead of the and operator &.
x = 1:10;
tooSmall = x < 4
tooBig = x > 7
reject = tooSmall | tooBig
keep1 = ~reject % Could also be written
keep2 = ~tooSmall & ~tooBig % not too small and not too big
x(reject) % 1, 2, and 3 are too small; 8, 9, and 10 are too big
x(keep1) % 4, 5, 6, and 7 are just right
참고 항목
카테고리
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!