[R] blockwise matrix multiplication
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Feb 9 15:19:45 CET 2007
Try this:
A * M[as.matrix(expand.grid(x,x))[,2:1]]
On 2/9/07, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
More information about the R-help
mailing list