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

1 Ansicht (letzte 30 Tage)
Oladunjoye
Oladunjoye am 22 Mär. 2014
Kommentiert: Oladunjoye am 24 Mär. 2014
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?

Akzeptierte Antwort

Mischa Kim
Mischa Kim am 22 Mär. 2014
Bearbeitet: Mischa Kim am 22 Mär. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by