Selecting pixels within a matrix

Suppose I have a matrix of land heights which range from 0-5000m.
How can i create a new matrix of the same size, that keeps only the values between 300-400, and sets all the rest to 0.
eg. Orignial matrix (completely hypothetical) = [0 100 300 800 5000; 200 400 600 1000 2000; 400 3000 4000 4000 5000; etc etc] New Matrix = [0 0 300 0 0; 0 400 0 0 0;400 0 0 0 0; etc ]
So far i have this: [m,n] = size(Land) for i = 1:m for j = 1:n if 300<Land(i,j)<400 matrix(i,j) = Land(i,j) else matrix(i,j) = 0 end end end
However this isnt even close to being right! Please help!Thank you!

1 comentario

philippa
philippa el 12 de Sept. de 2011
sorry my original code didnt come out right... it looks like this:
[m,n] = size(Land)
for i = 1:m
for j = 1:n
if 300<Land(i,j)<400
matrix(i,j) = Land(i,j)
else
matrix(i,j) = 0
end
end
end

Iniciar sesión para comentar.

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 12 de Sept. de 2011
A=[0 100 300 800 5000; 200 400 600 1000 2000; 400 3000 4000 4000 5000]
A(A<=300)=0;
A(A>=400)=0;

Categorías

Más información sobre Denoising and Compression en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Sept. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by