complex nested for loop

1 visualización (últimos 30 días)
katarado
katarado el 2 de Jun. de 2017
Comentada: katarado el 3 de Jun. de 2017
This is the section of my script that does not seem to be working (Full script attached). I get bestDx=-30, bestDy=-30, newCoeff=[1,0.45;0.45,1] , bestCoeff=[1,0.18;0.18,1].
This does not make sense as we need bestCoeff >= newCoeff, and I doubt the bestDx/Dy are the initial value. What am I missing?
for Dx = -30: 30
for Dy = -30: 30
%Limit range of x and y to 1:384
for x = 1:384
for y = 1:384
%Limit range of x+Dx and y+Dy to 1:384
if ((x+Dx<1) || (y+Dy<1) || (x+Dx>384) || (y+Dy>384))
continue
else
%weather1618Modified is the forecasted weather1823
weather1618Modified(x+Dx,y+Dy) = weather1618(x,y);
%Find the best correlation; Is corrcoef the right formula?
newCoeff=corrcoef(weather1623,weather1618Modified,'rows','pairwise');
if newCoeff>maxCoeff
maxCoeff=newCoeff;
bestDx=Dx;
bestDy=Dy;
end %end if
end %end if
end %end y
end %end x
end %end Dy
end %end Dx

Respuestas (1)

Guillaume
Guillaume el 3 de Jun. de 2017
I've not tried to understand your code much but this looks very suspicious:
maxCoeff=0;
%...
newCoeff=corrcoef(...);
if newCoeff>maxCoeff
maxCoeff=newCoeff;
newCoeff is a matrix. The first time that you're doing the if test. You're comparing a matrix to 0. The if succeeds if all elements of newCoeff are greater than 0. Afterwards, you've put a matrix in maxCoeff (which started as scalar!) and the if will only succeed if all the elements of newCoeff are greater than the corresponding elements of maxCoeff. I doubt that's what you meant to do but if it is then
if all(newCeoff(:) > maxCoeff(:))
would make that clear (or a comment stating the same).
  1 comentario
katarado
katarado el 3 de Jun. de 2017
Hello, you are right about that, thank you. I updated that to maxCoeff = [0,0;0,0]. But the problem seems to persist. And I tried your version (just in case) but none of the needed variables are created.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by