complex nested for loop

4 views (last 30 days)
katarado
katarado on 2 Jun 2017
Commented: katarado on 3 Jun 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

Answers (1)

Guillaume
Guillaume on 3 Jun 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 Comment
katarado
katarado on 3 Jun 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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!