How to collect output matrix from for loop

10 ビュー (過去 30 日間)
Singthong Pat
Singthong Pat 2021 年 9 月 29 日
コメント済み: Singthong Pat 2021 年 9 月 29 日
when I get 2 matrix or n matrix from for loop
clc
n = 2;
for i=1:n
A = randi(5,3,3)
end
how to A1+A2 (A1,A2 where i = 1,2)

採用された回答

Chunru
Chunru 2021 年 9 月 29 日
If you want to accumate over different matrices
n = 10;
A = zeros(3, 3)
A = 3×3
0 0 0 0 0 0 0 0 0
for i=1:n
A = A + randi(5,3,3);
end
A
A = 3×3
35 31 32 34 28 36 30 34 34
If you do want to access different matrices:
A = zeros(3, 3, n);
for i=1:n
A(:, :, i) = randi(5,3,3);
end
sum(A, 3)
ans = 3×3
29 29 25 31 30 30 34 29 35
  1 件のコメント
Singthong Pat
Singthong Pat 2021 年 9 月 29 日
Thank you , Mr.Chunru .
This A(:, :, i) allows me to fix my problem.

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

その他の回答 (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