0

I'd like to know if there is a way to combine two matrices with different dimensions into an array. I know there is the function abind(), but that function does not allow for different dimensions of the matrices. I need this because I use JAGS (with either R2jags or rjags) and these packages do not allow the data to be in list form. It does however work when I enter an array.

When I have the following two matrices:

a <- matrix(0, 3, 2)
b <- matrix(0, 4, 6)
a

     [,1] [,2]
[1,]    0    0
[2,]    0    0
[3,]    0    0

b

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0

I would ideally have it like this:

abind(a,b, along = 3)

, , 1

     [,1] [,2]
[1,]    0    0
[2,]    0    0
[3,]    0    0

, , 2

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0
Jolanda Kossakowski
  • 451
  • 2
  • 6
  • 14

2 Answers2

0

You can try to think of the matrices as one dimmensional arrays, which technically they are.

If you look at a matrix 'a' that is 3X3 and you try to access a[2][1] for example it is equivallent to a[2*3 + 1]. You can use this to just traverse your matrices and combine them.

Aksen P
  • 4,564
  • 3
  • 14
  • 27
in need of help
  • 1,606
  • 14
  • 27
-1
d=list(a,b)

i hope this is what do you want it's just like this