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

1 回表示 (過去 30 日間)
Oladunjoye
Oladunjoye 2014 年 3 月 22 日
コメント済み: Oladunjoye 2014 年 3 月 24 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by