简体   繁体   中英

What is the numpy equivalent for R's matrix function?

I'm porting over some R code and you can do something like this (x and centers are matrices):

nx <- nrow(x);
nc <- nrow(centers);
matrix(rowSums(x), nrow=nx, ncol=nc)

However numpy.matrix does not provide the nrow and ncol arguments. I tried using reshape, but you have to preserve the total number of elements (which does not seem to be the case for the R function).

How would I achieve the same effect in numpy?

Does rowSums(x) * numpy.ones([nx, nc]) do what you want? It does provide an array instead of a matrix, but that is often better in numpy anyway.

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