Averaging Multiple Matrices into a single matrix of same Dimensions

4 ビュー (過去 30 日間)
Dwijaraj
Dwijaraj 2023 年 8 月 17 日
回答済み: Steven Lord 2023 年 8 月 17 日
I am generating a matrix of 5x455832 dimensions. I want to generate it for a total of 500 times and want to take the average matrix and use it for further calculations.
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 8 月 17 日
Add the matrices and divide by 500. What seems to be the problem?

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

採用された回答

Steven Lord
Steven Lord 2023 年 8 月 17 日
Making a 3-dimensional array this size does consume a good chunk of memory, but if you have it available storing the matrices as pages of that array then calling mean over dimension 3 is an option. I'm creating random data in the example, but you could preallocate A using the zeros function and fill it in with your real data.
A = rand([5, 455832, 500]);
S = mean(A, 3);
Alternately you could keep two matrices around, one of your data and one accumulator. At each step where you create your data matrix, add it to the accumulator. At the end, divide by the number of steps. I'm using the same random data array and considering each page to be what was "created" at each step.
S2 = zeros(5, 455832);
n = 500;
for whichstep = 1:n
generatedData = A(:, :, whichstep);
S2 = S2 + generatedData;
end
S2 = S2 / n;
whos A S S2
Name Size Bytes Class Attributes A 5x455832x500 9116640000 double S 5x455832 18233280 double S2 5x455832 18233280 double
Let's check that S and S2 gave the same results.
check = norm(S-S2)
check = 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by