Replace values within a range to zero
146 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 28 Lug 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.
Risposte (1)
sudobash
il 28 Lug 2022
Modificato: sudobash
il 28 Lug 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 Commento
Steven Lord
il 28 Lug 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
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!