Storing outputs from loop iterations into a 1 dimensional array

1 view (last 30 days)
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!

Accepted Answer

Star Strider
Star Strider on 14 Mar 2015
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!