Storing outputs from loop iterations into a 1 dimensional array

Hi,
I need help storing iterations from a for loop into a 1 dimensional array in MATLAB. Here's my script:
M=input('Enter matrix M ');
K=input('Enter constant vector K ');
n=input('Enter number of unknown forces n ');
for i=1:n
CramerCalc(M,K,i);
end
And my function:
function [ F ] = CramerCalc( M,K,i )
D=det(M);
Mat=M;
Mat(:,i)=K(:);
C=det(Mat);
F=C/D;
V(i,:)=[F]
What I'm getting: V =
134.1989
V =
0
169.7423
V =
0
0
120.0259
V =
0
0
0
0
V =
0
0
0
0
60.0259
V =
0
0
0
0
0
120.0259
How can I put it all into one?
Also, if you can, could you please teach me how to code the for loop to store each answer under different names? For example, the answer from the first iteration would be stored as F1, the second as F2, so on and so forth.
Finally, how would I be able to combine the 2 previous things so that the array would come out like this: V=[F1 F2 F3 F4 F5 F6]
Thank you in advance for any help!

 채택된 답변

Star Strider
Star Strider 2015년 3월 14일
Take this line out of your function file (where it seems to be now):
V(i,:)=[F]
and put it in your loop (with a slight modification):
for i=1:n
V(i,:) = CramerCalc(M,K,i);
end
See if that helps.

댓글 수: 2

Omg that was the simplest solution! Thank you so much, that's incredible haha
My pleasure!

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

추가 답변 (0개)

카테고리

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

질문:

2015년 3월 14일

댓글:

2015년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by