Nested for loop not iterating
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a nested for loop needed to do a gridsearch to do a linear fit of a model with two unknown constants. As far as I can tell, these for loops are not iterating because i and j both show a value of 1 in the workspace. What am I doing wrong?
Thanks for any help.
agrid=[1:0.1:100];
bgrid=[.5:.0001:1.5];
lenagrid=length(agrid);
lenbgrid=length(bgrid);
for i = 1:lenagrid
for j = 1:lenbgrid
a_p = agrid(i)
b_p=bgrid(j);
fhat(i,j)=(a_p(i))-(b_p(j)).*magrange;
TSE(i,j)=sum((fhat-magrange).^2);
end
end
댓글 수: 2
답변 (1개)
per isakson
2019년 10월 10일
I'm just guessing. Replace
a_p = agrid(i)
b_p=bgrid(j);
fhat(i,j)=(a_p(i))-(b_p(j)).*magrange;
by
fhat(i,j) = ( agrid(i) - bgrid(i) ).*magrange;
댓글 수: 0
참고 항목
카테고리
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!