Borrar filtros
Borrar filtros

For loop isn't working, unsure as to why

1 visualización (últimos 30 días)
Robert
Robert el 19 de Abr. de 2016
Editada: James Tursa el 19 de Abr. de 2016
In the following code, 'forrest', 'shrub', 'sav', 'grass' and 'agri' have all been initialized at 35*43 matrices of NaNs.
'lut' and 'D10weight' are also 35,43 matrices. The idea is that each of 'forrest', 'shrub', 'sav', 'grass' and 'agri' get values from 'D10weight' where 'lut' equals a certain number. However they all remain as NaNs and I am unsure why.
for i = 1:35;
j = 1:43;
if lut(i,j) == 1
forrest(i,j) = D10weight(i,j);
end
if lut(i,j) == 2
shrub(i,j) = D10weight(i,j);
end
if lut(i,j) == 3
sav(i,j) = D10weight(i,j);
end
if lut(i,j) == 4
grass(i,j) = D10weight(i,j);
end
if lut(i,j) == 5
agri(i,j) = D10weight(i,j);
end
end

Respuesta aceptada

James Tursa
James Tursa el 19 de Abr. de 2016
Editada: James Tursa el 19 de Abr. de 2016
I think you meant for this to be a double for loop. As coded, you only have one for loop and j is a vector. E.g., is this what you meant?
for i = 1:35;
for j = 1:43;
if lut(i,j) == 1
forrest(i,j) = D10weight(i,j);
end
if lut(i,j) == 2
shrub(i,j) = D10weight(i,j);
end
if lut(i,j) == 3
sav(i,j) = D10weight(i,j);
end
if lut(i,j) == 4
grass(i,j) = D10weight(i,j);
end
if lut(i,j) == 5
agri(i,j) = D10weight(i,j);
end
end
end
  1 comentario
Robert
Robert el 19 de Abr. de 2016
Thanks, looks like that was the problem all along, it's obvious now that you've pointed it out. Thanks again

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 19 de Abr. de 2016
Here is a way 100% guaranteed to help you solve it: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Have you tried actually stepping through line by line and checking the value of lut(i,j) and seeing why your "if" statements never get true and so never assigns anything?

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by