Replace values within a range to zero
146 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 28 de Jul. de 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.
Respuestas (1)
sudobash
el 28 de Jul. de 2022
Editada: sudobash
el 28 de Jul. de 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 comentario
Steven Lord
el 28 de Jul. de 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
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!