How to ge the average of two matrices on Matlab?

4.2k views Asked by At

I have 2 matrices,

a= [1 2 3; 4 5 6; 7 8 9; 10 11 12];

and

b= [13 14 15; 16 17 18; 19 20 21; 22 23 24];

How can I make the average of these 2 matrices and store the values in another matrix "C" on Matlab?

The value of C will be,

c= [(1+13)/2 (2+14)/2 (3+15)/2; (4+16)/2 (5+17)/2 (6+18)/2;...]

Thanks.

2

There are 2 answers

0
Fantastic Mr Fox On

You would do:

c = (a+b)/2

This will give you the desired result.

0
rayryeng On

Another way would be to stack the matrices on top of each other in 3D, then find the average along the third dimension:

c = mean(cat(3, a, b), 3);