An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120220/d976bc7a/attachment.pl>
Column wise matrix multiplication
5 messages · Graziano Mirata, Dimitris Rizopoulos, (Ted Harding) +1 more
Try apply(A, 1, prod) I hope it helps. Best, Dimitris
On 2/20/2012 4:21 PM, Graziano Mirata wrote:
Hi all, I am trying to multiply each column of a matrix such to have a unique resulting vector with length equal to the number of rows of the original matrix. In short I would like to do what prod(.) function in Matlab does, i.e. A<-matrix(c(1:10),5,2) V = A[,1]*A[,2] Thank you Graziano [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
On 20-Feb-2012 Graziano Mirata wrote:
Hi all, I am trying to multiply each column of a matrix such to have a unique resulting vector with length equal to the number of rows of the original matrix. In short I would like to do what prod(.) function in Matlab does, i.e. A <-matrix(c(1:10),5,2) V = A[,1]*A[,2] Thank you Graziano
The Matlab prod(A,2) function computes the products along the
rows of the matrix A and returns the result as a column vector,
of length equal to the number of rows in A, which seems to be
what you describe.
Your code above does this for your 2-column example, but the
result is a simple "R vector" which is not an array (and in
particular is not a column vector):
A[,1]*A[,2]
# [1] 6 14 24 36 50
dim(A[,1]*A[,2])
# NULL
For a matrix A with arbitrary number of columns, if you wanted
the row sums rather than the row products, you could use the
R function rowSums():
rowSums(A)
# [1] 7 9 11 13 15
This is still a dimensionless "simple R vector":
dim(rowSums(A))
# NULL
Unfortunately, there seems to be no equivalent for products
(e.g. "rowProds"). But you can define one:
rowProds <- function(X){ apply(X,1,FUN="prod") }
rowProds(A)
# [1] 6 14 24 36 50
Even then, the result is a "simple R vector", without dimensions:
dim(rowProds(A))
# NULL
If you need an array (row) vector then you can apply t():
t(rowProds(A))
# [,1] [,2] [,3] [,4] [,5]
# [1,] 6 14 24 36 50
or t(t()) for a column vector:
t(t(rowProds(A)))
# [,1]
# [1,] 6
# [2,] 14
# [3,] 24
# [4,] 36
# [5,] 50
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 20-Feb-2012 Time: 17:54:13
This message was sent by XFMail
[See at end]
On 20-Feb-2012 Ted Harding wrote:
On 20-Feb-2012 Graziano Mirata wrote:
Hi all, I am trying to multiply each column of a matrix such to have a unique resulting vector with length equal to the number of rows of the original matrix. In short I would like to do what prod(.) function in Matlab does, i.e. A <-matrix(c(1:10),5,2) V = A[,1]*A[,2] Thank you Graziano
The Matlab prod(A,2) function computes the products along the
rows of the matrix A and returns the result as a column vector,
of length equal to the number of rows in A, which seems to be
what you describe.
Your code above does this for your 2-column example, but the
result is a simple "R vector" which is not an array (and in
particular is not a column vector):
A[,1]*A[,2]
# [1] 6 14 24 36 50
dim(A[,1]*A[,2])
# NULL
For a matrix A with arbitrary number of columns, if you wanted
the row sums rather than the row products, you could use the
R function rowSums():
rowSums(A)
# [1] 7 9 11 13 15
This is still a dimensionless "simple R vector":
dim(rowSums(A))
# NULL
Unfortunately, there seems to be no equivalent for products
(e.g. "rowProds"). But you can define one:
rowProds <- function(X){ apply(X,1,FUN="prod") }
rowProds(A)
# [1] 6 14 24 36 50
Even then, the result is a "simple R vector", without dimensions:
dim(rowProds(A))
# NULL
If you need an array (row) vector then you can apply t():
t(rowProds(A))
# [,1] [,2] [,3] [,4] [,5]
# [1,] 6 14 24 36 50
or t(t()) for a column vector:
t(t(rowProds(A)))
# [,1]
# [1,] 6
# [2,] 14
# [3,] 24
# [4,] 36
# [5,] 50
Ted.
-------------------------------------------------
Further to the above: I have managed to track down a function rowProds in the "matrixStats" package: http://finzi.psych.upenn.edu/R/library/matrixStats/html/rowProds.html http://www.stats.bris.ac.uk/R/web/packages/matrixStats/matrixStats.pdf Note that: "Details Internally the product is calculated via the logarithmic transform, treating zeros and negative values specially." In view of this, which strikes me as potentially getting close to thin ice, plus the overhead of loading a whole package just for one function, it may be more straightforward (and perhaps safer) to define one's own function (as above). Also (see the PDF reference manual) it is apparently "work in progress" and also has dependencies on other packages: see the description at http://www.stats.bris.ac.uk/R/web/packages/matrixStats/index.html Ted. ------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 20-Feb-2012 Time: 21:33:25 This message was sent by XFMail
On Mon, Feb 20, 2012 at 09:33:28PM -0000, Ted Harding wrote:
[...]
Unfortunately, there seems to be no equivalent for products
(e.g. "rowProds"). But you can define one:
rowProds <- function(X){ apply(X,1,FUN="prod") }
rowProds(A)
# [1] 6 14 24 36 50
Even then, the result is a "simple R vector", without dimensions:
dim(rowProds(A))
# NULL
If you need an array (row) vector then you can apply t():
t(rowProds(A))
# [,1] [,2] [,3] [,4] [,5]
# [1,] 6 14 24 36 50
or t(t()) for a column vector:
t(t(rowProds(A)))
# [,1]
# [1,] 6
# [2,] 14
# [3,] 24
# [4,] 36
# [5,] 50
Ted.
-------------------------------------------------
Further to the above: I have managed to track down a function rowProds in the "matrixStats" package: http://finzi.psych.upenn.edu/R/library/matrixStats/html/rowProds.html http://www.stats.bris.ac.uk/R/web/packages/matrixStats/matrixStats.pdf Note that: "Details Internally the product is calculated via the logarithmic transform, treating zeros and negative values specially." In view of this, which strikes me as potentially getting close to thin ice, plus the overhead of loading a whole package just for one function, it may be more straightforward (and perhaps safer) to define one's own function (as above). Also (see the PDF reference manual) it is apparently "work in progress" and also has dependencies on other packages: see the description at http://www.stats.bris.ac.uk/R/web/packages/matrixStats/index.html
Hi.
Computing the componentwise product of the columns may be
done also as follows.
rowProds2 <- function(a) Reduce("*", as.data.frame(a))
For a matrix with a large number of rows, this is more
efficient, since it contains a cycle over columns and
not a cycle over rows as apply(a, 1, prod).
rowProds <- function(X){ apply(X,1,FUN="prod") }
m <- 100000
a <- matrix(sample(0 + 1:10, 10*m, replace=TRUE), nrow=m, ncol=10)
t1 <- system.time( out1 <- rowProds(a))
t2 <- system.time( out2 <- rowProds2(a))
identical(out1, out2)
[1] TRUE
rbind(t1, t2)
user.self sys.self elapsed user.child sys.child
t1 0.550 0.003 0.553 0 0
t2 0.049 0.013 0.062 0 0
Petr Savicky.