B Seven
B Seven

Reputation: 45943

How to get the mean of rows of a matrix in Octave?

>> a = [2,3,4;6,7,8]
a =

   2   3   4
   6   7   8

>> mean(a)
ans =

   4   5   6

where [4 5 6] is the mean for each column

How can I get the mean for each row?

In my example, I would expect [3;7]

Upvotes: 17

Views: 31129

Answers (3)

Shekar
Shekar

Reputation: 31

You can do mean (a, 2) returns : [3; 7] Trick is the 2nd parameter specifies along which dimension you want mean. 1 is default ("Column").

Upvotes: 0

Jeff Hykin
Jeff Hykin

Reputation: 2627

Alternatively to the other answer, you can simply use the transpose feature

>> a' 
ans =     

     2  6
     3  7
     4  8

>>  mean(a')
ans = 

     3  7

I suggest this answer over the other because it works for any row based octave function (max , min , sum , etc)

Upvotes: 5

NPE
NPE

Reputation: 500357

From http://www.mathworks.co.uk/help/techdoc/ref/mean.html:

For matrices, mean(A,2) is a column vector containing the mean value of each row.

In Octave it's the same.

Upvotes: 25

Related Questions