Add each element of a matrix

15 views (last 30 days)
Debanjan Maity
Debanjan Maity on 1 Dec 2018
Edited: Image Analyst on 1 Dec 2018
I have 5 matrices of size 59*52. I want to add each respective elements of different matrices and get a single matrix of size 59*52.
Say the matrices are
m1 =[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]
m2 = [5 4 3 2 1; 10 9 8 7 6; 15 14 13 12 11; 20 19 18 17 16]
The result will come as
result =[16 16 16 16 16; 16 16 16 16 16;26 26 26 26 26;36 36 36 36 36]
  3 Comments
madhan ravi
madhan ravi on 1 Dec 2018
The example you gave is not clear please provide a clear example which elements are added and what is the pattern?
Image Analyst
Image Analyst on 1 Dec 2018
Edited: Image Analyst on 1 Dec 2018
madhan is absolutely right. Here are your matrices:
m1 =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
m2 =
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
20 19 18 17 16
result =
16 16 16 16 16
16 16 16 16 16
26 26 26 26 26
36 36 36 36 36
Specifically, can you explain why the top row is all 16s? Is the rule something like
result = m1+m2;
result(1,:) = result(2,:); % Make first row the same as the second.
Or maybe
minValues = min(result, 1);
result(1, :) = minValues;
Surely it's not something as trivial as simply adding the two matrices. You can't be asking something that trivial since that's what you learn pretty much in the first 5 minutes of learning MATLAB (if it is, see KALYAN's answer below). So, again, what is the rule?

Sign in to comment.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 1 Dec 2018
Edited: KALYAN ACHARJYA on 1 Dec 2018
I supposed you are asking simple addition. Say matrix1, matrix2,matrix3,matrix4,matrix5
Matrix addition result the same size matrix.
sum_matrix=matrix1+matrix2+matrix3+matrix4+matrix5
Also your example case
>> k1=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]
k1 =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
>> k2=[5 4 3 2 1; 10 9 8 7 6; 15 14 13 12 11; 20 19 18 17 16]
k2 =
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
20 19 18 17 16
>> k3=k1+k2
k3 =
6 6 6 6 6
16 16 16 16 16
26 26 26 26 26
36 36 36 36 36
>>

Tags

Community Treasure Hunt

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

Start Hunting!