nest loop
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello,
Here is my code:-
clear all
x = 1:3;
y = 1:3;
a = 1:3;
b = 1:3;
for i = 1:3;
for j = 1:3;
a_pred = a(:,i);
b_pred = b(:,j);
y_pred = a_pred + b_pred.* x;
chi2 = (y-y_pred).^2;
end
end
how would I amend the nested loop so that every single combination of a and b values are calculated in y_pred please? i.e. a(1) and b(1) then a(2) and b(1) all the way to a(3) and b(3).
Thanks
채택된 답변
Matt Fig
2011년 3월 29일
1 개 추천
Why do you think all aren't being calculated? Here is a modified version of your code, not changing the mechanics, which shows what you are getting.
a = 1:3;
b = 1:3;
for i = 1:3;
for j = 1:3;
a_pred = a(:,i);
b_pred = b(:,j);
disp([a_pred b_pred])
end
end
.
.
EDIT
.
Here is how to store all the results in chi2:
x = 1:3;
y = 1:3;
a = 1:3;
b = 1:3;
chi2 = zeros(9,3);
cnt = 0;
for i = 1:3;
for j = 1:3;
cnt = cnt + 1;
a_pred = a(:,i);
b_pred = b(:,j);
y_pred = a_pred + b_pred.* x
chi2(cnt,:) = (y-y_pred).^2;
end
end
chi2
IDX = npermutek(1:3,2);
y_pred = bsxfun(@plus,a(IDX(:,1)), bsxfun(@times,b(IDX(:,2)),x.'));
chi2 = bsxfun(@minus,y,y_pred.').^2
Not that it should be faster here...
댓글 수: 11
Dominic Lawson
2011년 3월 29일
Dear Matt,
Thanks for getting back to me so quickly. Is it the case they are being calculated but are being over written and not stored in a matrix? The [] help store the numbers in a matrix?
Walter Roberson
2011년 3월 29일
chi2(i,j) = (y-y_pred).^2;
Matt Fig
2011년 3월 29일
As Walter shows, you are overwriting chi2 every loop iteration. For best results, pre-allocate your chi2 matrix before the loops.
chi2 = zeros(3);
for ...
for ..
...
...
chi2(i,j) = ...
end
end
Also, I recommend you get in the practice of _not_ using i and j as loop indices as they will mask the built-in MATLAB functions i and j.
Walter Roberson
2011년 3월 29일
To nit-pick: the built-in values for i and j are variables, not functions.
Matt Fig
2011년 3월 29일
But, they are functions too!
which i
which j
Also, see the help...
Matt Fig
2011년 3월 29일
Actually, since (y-y_pred).^2 is a row vector, storing in chi2(i,j) will not work. You will have to use a cell array and/or do some fancy indexing to save all these rows. See my edit above.
Walter Roberson
2011년 3월 29일
You are right, Matt. I got confused because class(i) returns 'double'. But of course with them being functions, naming them is equivalent to invoking them with no arguments, so that is what is done when I ask for the class.
Another way to see that it is built in is,
functions(@i)
the "file" field of which reads 'MATLAB built-in function' which would not be the case if i was a plain variable.
Matt Fig
2011년 3월 29일
I didn't know about the FUNCTIONS function, thanks Walter. That could come in handy!
Matt Fig
2011년 3월 29일
BTW, what does the second field, 'type' mean here? I see REPMAT is simple, so is PLOT!
Walter Roberson
2011년 3월 29일
I found it by using methods(@i)
Walter Roberson
2011년 3월 29일
Good question about 'type'. I was not able to find anything that did not indicate 'simple' there. I did find, though, that functions will use an empty file name for methods that do not have a file of their own. For example, functions(@func2str) where which shows func2str as if it is part of a file that does not actually exist.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
태그
참고 항목
2011년 3월 29일
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
