Finding an average matrix from each matrix in an array

I have an array of matrices, each with the same dimensions, I am looking to get one matrix which has the average value of all the matrices at each point, any advice on how to do this?

1 件のコメント

Joseph Cheng
Joseph Cheng 2021 年 7 月 13 日
i think the matlab coding help would depend on how you've arranged your array of matrices. but over all if you have matrix of N dimensions and you want the index average across the different matrices; the simplest would be to just add all these together to a single N dimensioned matrix and divide that by total number of matrices.

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

 採用された回答

Image Analyst
Image Analyst 2021 年 7 月 13 日

0 投票

averageMatrix = (matrix1 + matrix2 + matrix3) / 3;

4 件のコメント

Roisin Coveney
Roisin Coveney 2021 年 7 月 13 日
Thanks, Its just I will have say 1000 matrices in each array so didn't want to write out each one, can I use a for loop for that?
Image Analyst
Image Analyst 2021 年 7 月 13 日
Yes. If you have a cell array, ca, with 1000 cells
numberOfCells = numel(ca) % works for either 1D or 2D or more cell arrays.
sumMatrix = zeros(size(ca{1})); % Initialize to all zeros.
for k = 1 : numberOfCells
sumMatrix = sumMatrix + ca{k};
end
% Compute the average from the mean.
averageMatrix = sumMatrix / numberOfCells;
imshow(averageMatrix, []); % Display the average matrix in an axes control.
Note that the matrices in all cells must be the same size, otherwise you can't add them.
But a big question is why the matrices ended up in a cell array instead of a regular numerical array, like a 3-D double array. Cell arrays are slow, inefficient, memory hogs. I think a structure array or table would also be less efficient than a 3-D double array.
Roisin Coveney
Roisin Coveney 2021 年 7 月 14 日
Thank you! My data isn't in a cell array it is in a structural array, when I use that code the following message comes up
Brace indexing is not supported for variables of this type.
Roisin Coveney
Roisin Coveney 2021 年 7 月 14 日
Thanks, just changed it a bit and thats working now!

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

その他の回答 (1 件)

Jan
Jan 2021 年 7 月 13 日
編集済み: Jan 2021 年 7 月 13 日

1 投票

Which kind of array contains the matrices? A cell array, a numerical 3D array, a struct array? The solution depends on this detail.
A = {rand(4), rand(4), rand(4)}; % Cell array
meanA = sum(cat(3, A{:}), 3);
B = rand(3, 4, 4); % 3 4x4 matrices in numerical 3D array
meanB = reshape(sum(B, 1), 4, 4);
C(1).data = rand(4); % Struct array
C(2).data = rand(4);
C(3).data = rand(4);
meanC = sum(cat(3, C.data), 3)

1 件のコメント

Roisin Coveney
Roisin Coveney 2021 年 7 月 14 日
the data is contained in a structural array

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by