Saving a vector in a sub for-loop in the main loop

Hello,
I am trying to save the results obtained in the sub loop to the main loop but seems I am getting some errors; below is an example of what I need:
values=zeros(1,3);
for x=1:3
result=zeros(1,10);
for y=1:10
equation1=rand; %just for simplicity
result(y)=equation1; %This is what I want to save in the main loop; in this case it would consist of 30 elements
end
equation2=sum(result);
values(x)=equation2;
end
Any ideas are appreciated!
Thanks in advance.

 채택된 답변

Matt J
Matt J 2013년 5월 25일
편집: Matt J 2013년 5월 25일

0 개 추천

result=zeros(10,3);
for x=1:3
for y=1:10
equation1=.....
result(y,x)=equation1;
end
end
values=sum(result,1);

댓글 수: 3

AND
AND 2013년 5월 25일
Dear Matt,
Many thanks for your quick answer- but it doesn't seem to work. I have even tried result(x,y)- same it gives different answers but the problem in both seems to be that the older values (first 10), are deleted when recording the new ones.
Beside- should I modify result=zeros(1,10); at the beginning as the vector now would consist of 30 elements?
note: I have edited my question so that equation1=rand; just for simplicity- if you just copy the code and paste it into Matlab, you would see the problem!
Thanks again and regards
Matt J
Matt J 2013년 5월 25일
should I modify result=zeros(1,10); at the beginning as the vector now would consist of 30 elements?
You will see in my version of the code that I already do initialize result to have 30 elements.
AND
AND 2013년 5월 25일
Thanks alot Matt- Much Appreciated :).
Have a good evening//

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

AND
2013년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by