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

1 vue (au cours des 30 derniers jours)
Oladunjoye
Oladunjoye le 22 Mar 2014
Commenté : Oladunjoye le 24 Mar 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?

Réponse acceptée

Mischa Kim
Mischa Kim le 22 Mar 2014
Modifié(e) : Mischa Kim le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by