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

1 次查看(过去 30 天)
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?

采纳的回答

Mischa Kim
Mischa Kim 2014-3-22
编辑:Mischa Kim 2014-3-22
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by