complex nested for loop

조회 수: 3 (최근 30일)
katarado
katarado 2017년 6월 2일
댓글: katarado 2017년 6월 3일
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

답변 (1개)

Guillaume
Guillaume 2017년 6월 3일
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
katarado
katarado 2017년 6월 3일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by