简体   繁体   中英

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]

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.

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)

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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM