[R] combining matrices

Peter Ehlers ehlers at ucalgary.ca
Thu Apr 21 17:45:20 CEST 2011


On 2011-04-21 07:03, Marten Winter wrote:
> Heja,
>
> I hope someone is still there to help me:
>
> How can I somehow merge/combine matrices to get such a result:
>
> Matrix A
>
> A B
> x1 1 0
> x2 1 1
>
> Matrix B
>
> C D
> x3 1 0
> x4 0 1
>
> Resulting Matrix?
>
>
> A B C D
> x1 1 0 0 0
> x2 1 1 0 0
> x3 0 0 1 0
> x4 0 0 0 1
>

If you don't need this very often or very general, then use

  mat <- diag(0,4)
  mat[1:2, 1:2] <- A
  mat[3:4, 3:4] <- B
  mat

If you do need more flexibility, you could use the 'assist'
package's function bdiag() which handles block-diagonal
matrices:

  require(assist)
  mat <- bdiag(list(A,B))

Peter Ehlers

>
> Does anyone see this probably obvious solution with R how to do this?
> Sorry if this question is silly...it's too hot here to think properly ;O)
>
> Thanks!
> cheers
> Marten
>



More information about the R-help mailing list