Replace values within a range to zero
135 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 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.
Antworten (1)
sudobash
am 28 Jul. 2022
Bearbeitet: sudobash
am 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 Kommentar
Steven Lord
am 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
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!