complex nested for loop
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!