how to save an array in each iteration?

4 ビュー (過去 30 日間)
Dany
Dany 2020 年 10 月 19 日
コメント済み: Torsten K 2020 年 10 月 19 日
I have a parameter L and the following array SR:
L = 3;
SR = [ 0.0941 0.1129 0.0471 0 0 0
0.0941 0 0 0.0471 0.0941 0
0 0 0 0.0471 0 0.0471
0 0.1129 0 0 0.0941 0
0 0 0.0471 0 0 0.0471
0 0 0 0 0 0 ]
The code is:
for i = 1:L
A = - (1 / 2) * ( ( 2 * i - 1 ) / L) * SR
end
the result is. For each iteration a block.
A{1} = [ - 0.0157 - 0.0188 - 0.0078 0 0 0
- 0.0157 0 0 -0.0078 -0.0157 0
0 0 0 -0.0078 0 -0.0078
0 - 0.0188 0 0 -0.0157 0
0 0 - 0.0078 0 0 -0.0078
0 0 0 0 0 0 ]
A{2} = [ -0.0471 -0.0565 -0.0235 0 0 0
-0.0471 0 0 -0.0235 -0.0471 0
0 0 0 -0.0235 0 -0.0235
0 -0.0565 0 0 -0.0471 0
0 0 -0.0235 0 0 -0.0235
0 0 0 0 0 0 ]
A{3} = [ -0.0784 -0.0941 -0.0392 0 0 0
-0.0784 0 0 -0.0392 -0.0784 0
0 0 0 -0.0392 0 -0.0392
0 -0.0941 0 0 -0.0784 0
0 0 -0.0392 0 0 -0.0392
0 0 0 0 0 0]
The question is, how can I save a total array with all those three blocks?..
As the following configuration.
A = [ A{1} A{2} A{3} ]

採用された回答

Torsten K
Torsten K 2020 年 10 月 19 日
Here a variant with a 3d-matrix:
clearvars;
A = zeros(6,6,3)
L = 3;
SR = [ 0.0941 0.1129 0.0471 0 0 0
0.0941 0 0 0.0471 0.0941 0
0 0 0 0.0471 0 0.0471
0 0.1129 0 0 0.0941 0
0 0 0.0471 0 0 0.0471
0 0 0 0 0 0 ]
for i = 1:L
A(:,:,i) = - (1 / 2) * ( ( 2 * i - 1 ) / L) * SR
end

その他の回答 (2 件)

Torsten K
Torsten K 2020 年 10 月 19 日
Maybe a cell-array is what you are looking for?
A = cell(3,1);
L = 3;
SR = [ 0.0941 0.1129 0.0471 0 0 0
0.0941 0 0 0.0471 0.0941 0
0 0 0 0.0471 0 0.0471
0 0.1129 0 0 0.0941 0
0 0 0.0471 0 0 0.0471
0 0 0 0 0 0 ]
for i = 1:L
A{i,1} = - (1 / 2) * ( ( 2 * i - 1 ) / L) * SR
end
  4 件のコメント
madhan ravi
madhan ravi 2020 年 10 月 19 日
編集済み: madhan ravi 2020 年 10 月 19 日
A = cell(3, 1); % this is missing from your copied code, COPY IT properly
Dany
Dany 2020 年 10 月 19 日
in other words, I want to get this total matrix !!
A = [
-0.0157 -0.0188 -0.0078 0 0 0 -0.0471 -0.0565 -0.0235 0 0 0 -0.0784 -0.0941 -0.0392 0 0 0
-0.0157 0 0 -0.0078 -0.0157 0 -0.0471 0 0 -0.0235 -0.0471 0 -0.0784 0 0 -0.0392 -0.0784 0
0 0 0 -0.0078 0 -0.0078 0 0 0 -0.0235 0 -0.0235 0 0 0 -0.0392 0 -0.0392
0 -0.0188 0 0 -0.0157 0 0 -0.0565 0 0 -0.0471 0 0 -0.0941 0 0 -0.0784 0
0 0 -0.0078 0 0 -0.0078 0 0 -0.0235 0 0 -0.0235 0 0 -0.0392 0 0 -0.0392
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]

サインインしてコメントする。


Dany
Dany 2020 年 10 月 19 日
Thank you, so much!!!
  1 件のコメント
Torsten K
Torsten K 2020 年 10 月 19 日
You are welcome! :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by