Replace values within a range to zero
146 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 28 Jul 2022
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.
Answers (1)
sudobash
on 28 Jul 2022
Edited: sudobash
on 28 Jul 2022
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 Comment
Steven Lord
on 28 Jul 2022
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
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!