Abdullah Al Mubarok
Abdullah Al Mubarok

Reputation: 91

Mean matrix of matrices in matlab

i'm new to matlab and I'd like to ask something. Is there any function in matlab that allow me to make mean matrix of matrices?

To give you some picture, for example I have matrices like this :

A = [ 1 2
      3 4 ]

B = [ 3 2 
      1 2 ]

and then what I want for the mean matrix that I meant earlier is like this

Mean = [ 2 2
         2 3 ]

Anyone have a suggestion?

Upvotes: 1

Views: 8863

Answers (2)

bla
bla

Reputation: 26069

another option:

 mean([A(:) B(:)]')

 ans =

        2     2     2     3

This will make a vector from the two matrices and return a vector of the mean you wanted, you can then reshape it back to 2x2 using reshape,

reshape(mean([A(:) B(:)]'),size(A))

ans =

     2     2
     2     3

Edit: Eitan suggested the following one-liner solution that is more efficient:

mean(cat(ndims(A) + 1, A, B), ndims(A) + 1)

Upvotes: 5

Sameh K. Mohamed
Sameh K. Mohamed

Reputation: 2333

You can do it simply like the following:

a = [1 2 ;...
     3 4];

b = [1 3 ;...
     1 2];

sum = a + b;

mean = sum ./ 2;

And it will be:

mean =

    1.0000    2.5000
    2.0000    3.0000

Upvotes: 3

Related Questions