How to sum up multiple matrices, element by element

So I've got multiple 100x100 matrices saved as a multidimensional Array a. Now I want to sum them up, element by element so the result is one 100x100 matrix. Since I got n matrices, I want to have a loop or similar, so I don't have to call every matrix by name like A1 + A2 + A3 + A4 ... = A. Example:
A =
1 1 1
2 2 2
3 3 3
B =
4 4 4
5 5 5
6 6 6
C =
7 7 7
8 8 8
9 9 9
D = [Some magical loop]
D =
12 12 12
15 15 15
18 18 18

1 件のコメント

Stephen23
Stephen23 2017 年 11 月 10 日
編集済み: Stephen23 2017 年 11 月 10 日
Your question contradicts itself: do you either have "as a multidimensional Array a" or do you have lots of separate matrices named "A1 + A2 + A3 + A4 ... " ?
If you have one ND array then you do not need lots of separate arrays. If you have lots of separate arrays then you really need one ND array!

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

 採用された回答

Jan
Jan 2017 年 11 月 11 日
編集済み: Jan 2017 年 11 月 11 日

3 投票

If you really have a list of variables A1, A2, A3: This is a bad idea and impedes using the data. Prefer a multi-dimensional array A(m, n, k). Then the sum is trivial:
A = cat(3, ...
[1 1 1; ...
2 2 2; ...
3 3 3], ...
[4 4 4; ...
5 5 5; ...
6 6 6], ...
[7 7 7; ...
8 8 8; ...
9 9 9]);
D = sum(A, 3);

8 件のコメント

Kuba
Kuba 2017 年 11 月 11 日
Thank you, that was my first thought of using 'sum', but I was missing the example on the matworks side for 'sum' with dimension 3.
Image Analyst
Image Analyst 2017 年 11 月 11 日
Since you said you've already done the concatenation cat(3, a1,a2,etc) and "saved as a multidimensional Array a." you'd simply do
sumMatrix = sum(a, 3);
I'd encourage you not to make your code look like an alphabet soup of single letter variable names and use more descriptive names than "a". Since Jan's code solved your question, can you please "Accept this answer" by clicking on the link?
Jairo C Peralta
Jairo C Peralta 2019 年 2 月 5 日
What if i havent done the concatenation?
Jan
Jan 2019 年 2 月 6 日
@Jairo: If you haven't done the concatenation yet, do it now. What is your problem?
Stephanie Watermann
Stephanie Watermann 2022 年 3 月 14 日
Is it also possible to build the sum only over a specific range of "pages" in the third dimension? I couldn't find any solution for this. For example, I have a multidimensional array "Spectra" of size (256 * 2048 * 26) and depending on the user input I want to build the sum over all values in the first and second dimension but only e.g. from page 3-6 in the third dimension.
Jan
Jan 2022 年 3 月 14 日
編集済み: Jan 2022 年 3 月 14 日
Do you mean this:
x = rand(256, 2048, 26);
y = sum(x(:, :, 3:6), 'all')
% or
y = sum(x(:, :, 3:6), 3)
Stephanie Watermann
Stephanie Watermann 2022 年 3 月 15 日
Thank you so much!
Jan
Jan 2022 年 3 月 15 日
You are welcome.

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

その他の回答 (1 件)

Birdman
Birdman 2017 年 11 月 10 日
編集済み: Birdman 2017 年 11 月 10 日

2 投票

A(:,:,1)=randi([1 3],100,100);
A(:,:,2)=randi([1 3],100,100);
A(:,:,3)=randi([1 3],100,100);
A(:,:,4)=randi([1 3],100,100);
B=zeros(size(A,1),size(A,2));
for i=1:size(A,3)
B=B+A(1:size(A,1),1:size(A,2),i);
end
disp(B)

2 件のコメント

Kuba
Kuba 2017 年 11 月 10 日
Thanks a lot, it works perfectly!!
Jan
Jan 2017 年 11 月 11 日
Note that
B = B + A(1:size(A,1),1:size(A,2),i);
can be processed much more efficient when written as:
B = B + A(:, :, i);
But the complete code can be simplified to:
A = randi([1 3], 100, 100, 4);
B = sum(A, 3);

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2017 年 11 月 10 日

コメント済み:

Jan
2022 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by