How to store the results from multiple "for loops" as a matrix

1 view (last 30 days)
Hi everyone,
I have a code which outputs a vector and the function value(fvals). The code contains a for loop and the result from the code is stored in a matrix P, where P is a j x 2 matrix, j the number of iteration. The first column of P is the vector, which has length six(6) ,and the second column is fvals. Now I want to add another "for loop" to iterate over k such that P will become a j x 2k matrix i.e. the results for each iteration of the new for loop the solution will be store as new columns in P.
Shown below is a part P at present. What I want is to add new columns by iterating over k.
first column(vector) second column(fvals)
[0;0;-0.00316499454647002;-0.00316499454647025;1.04719755119660] -2.04
[0;0;-0.00316499454647002;-0.00316499454647025;2.09439510239320] -2.04
[0;0;-0.00316499454647002;-0.00316499454647025;3.14159265358979] -2.04
How can I do this?

Accepted Answer

Mischa Kim
Mischa Kim on 22 Mar 2014
Edited: Mischa Kim on 22 Mar 2014
Oladunjoye, use someting like:
P = zeros(njj,nkk);
for jj = 1:njj
...
P(jj,1:7) = [col1 col2]; % col1 is the 1-by-6
for kk = 1:nkk
...
P(jj,7*kk+1:7*kk+7) = [col11 col22];
end
end
Note, that the 1-by-6 ( col1 ) needs to be saved in 6 columns.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!