[R] blockwise matrix multiplication

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Feb 9 14:27:57 CET 2007


Hi

Given an n-by-n  matrix A, say n=10 and

A <- matrix(1:100,10,10)

and a vector x of length n where 1 <=x[i] <= n for i=1..n
say

x <- c(1,1,1,2,4,3,3,3,4,4)

and a matrix M of size max(x)-by-max(x), say

M <- matrix(c(1, 0.1, 0, 0.2, 0.1, 1, 0, 0, 0, 0, 1, 0.2, 0.2,
0, 0.2, 1),4,4)

how do I partition A according to the equivalence classes
of the elements  of x and  "block multiply"  by M?

I want

for(i in 1:4){for(j in 1:4){
   A[which(x==i),which(x==j)] <-  A[which(x==i),which(x==j)]*M[i,j]
}}


Is there a better way than this ghastly for() loop?


--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743



More information about the R-help mailing list