简体   繁体   中英

How to build mean of matrix rows to create a new row?

Lets assume we have got the matrix M = np.array([[1,2,3],[4,5,6],[7,8,9]]) . I want to create a new row that conclumerates row number 1 and row number 2. So the output should be [2.5, 3.5, 4.5]. To compute the mean between rows 1 and 2 I tried: np.mean(M[0:2,:]) which returns 3.5 as the scalar mean of both rows. I want to know how to achieve the element wise mean between element M[i,j] denoted by ij, so my resulting new row will be [0.5(00+10), 0.5(01+11), 0.5(02+12)].

正如@Ch3steR 建议使用 np.mean(M[0:2,:], axis=0)。

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