[R] Matrix multiplication
Ling, Gary (Electronic Trading)
Gary_Ling at ml.com
Wed Aug 6 23:41:39 CEST 2008
Hi,
Yes. this is a way, and relatively easy ... Using "Reduce". Note: in
order to use "Reduce", you need an update-to-date version of R. I'm
using 2.6.2.
First you have to have all the matrices line up with correct dimensions,
i.e. [m_1 x m_2] %*% [m_2 x m_3] %*% ... %*% [m_(n-1) x m_n] (u get my
idea, right?) And I'll use all 4x4 square matrices as an example below.
### demo ###
# simulate a list of 4x4 matrices; 10 matrices
list.of.matrices <- replicate(10,matrix(rnorm(16),4),simplify=FALSE)
# power of "Reduce"!!! (functional programming)
Reduce( "%*%", list.of.matrices )
# [,1] [,2] [,3] [,4]
# [1,] 3.2494000 67.789481 49.703126 39.731644
# [2,] -0.2992145 -39.928493 -24.463129 30.178451
# [3,] -0.2735238 -7.953889 -6.130237 -9.717463
# [4,] -0.7801092 3.003660 1.104163 -5.834807
# let's check ... (imperative programming)
MM <- list.of.matrices[[1]]; for (M in list.of.matrices[-1]) { MM <- MM
%*% M }
MM
# [,1] [,2] [,3] [,4]
# [1,] 3.2494000 67.789481 49.703126 39.731644
# [2,] -0.2992145 -39.928493 -24.463129 30.178451
# [3,] -0.2735238 -7.953889 -6.130237 -9.717463
# [4,] -0.7801092 3.003660 1.104163 -5.834807
### end demo ###
So, they are the same. Cheers!
-gary
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Zhang Yanwei - Princeton-MRAm
Sent: Wednesday, August 06, 2008 5:02 PM
To: r-help at r-project.org
Subject: [R] Matrix multiplication
Hi all,
Is there an easy way to do cumulative matrix multipliation in R?
What's the syntex? Thanks.
Sincerely,
Yanwei Zhang
Department of Actuarial Research and Modeling
Munich Re America
Tel: 609-275-2176
Email: yzhang at munichreamerica.com<mailto:yzhang at munichreamerica.com>
[[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.
--------------------------------------------------------
This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing.
More information about the R-help
mailing list