-2

Suppose I have 3 matrices C,W, and S

C <- matrix(1:3)
W <- matrix(2:4)
S <- matrix(3:5)

I want to make a matrix with those matrices as elements. Say matrix K, but each element of matrix K being a matrix itself. Just as a list of matrices works, but instead in a matrix form. I.e.:

> K
      [,1] [,2] [,3]
[1,]    C    0   0
[2,]    0    W   S

C, W and S would each be matrix objects stored inside the larger matrix K.

Ultimately, I would like to be able to then use matrix multiplication like K %*% K or similar.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
Joss_at
  • 59
  • 1
  • 1
  • 2
  • The matrix didnt show up as i wantes, but its a 2X3 matrix with first row [ C 0 0] and second row [ 0 W S] – Joss_at Apr 08 '14 at 00:09
  • Welcome on SO. Please read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and `?array`. – sgibb Apr 08 '14 at 00:10
  • 1
    I have no idea *why* you'd want this, but you can make a matrix of a list like `matrix(list(C,0,0,W,0,S),nrow=2)`. But it's not really practical. – thelatemail Apr 08 '14 at 00:30
  • Can you provide an example of the desired output – CMPS Apr 08 '14 at 00:38
  • The output i would like is just as the one in the question, just that each letter is a matrix by itself. Not a matrix with ALL the elements of the other matrix but a matrix where each element is a matrix – Joss_at Apr 08 '14 at 00:49
  • @user3508921 - does my suggestion answer your question? It works, that much I'm sure of, but I don't know if that is what you intended. – thelatemail Apr 08 '14 at 01:12
  • Is does work! to present the matrices as wanted thank you! – Joss_at Apr 08 '14 at 01:18
  • But i have an aditional question! If i had that matrix, plus another one (say the transpose of that one). How can i do to make the product of them? cause i get this msg: "Error in A %*% B : requires numeric/complex matrix/vector arguments" – Joss_at Apr 08 '14 at 01:19
  • @user3508921 - I've edited your question to include that component. It is hopefully answerable now. – thelatemail Apr 08 '14 at 01:29
  • In order to make that work you would need to define an Ops entry for %*% that was dispatched on list objects. – IRTFM Apr 08 '14 at 01:37
  • 1
    Are you trying to define block matrices? – Spacedman Apr 08 '14 at 09:13

1 Answers1

2

There are not a lot of classes than can be an element in an R matrix. In particular objects that rely on attributes for their behavior cannot be objects that will retain their essential features. And ironically that includes matrices themselves since their behavior is governed by the dim(ension) attribute. That exclusion applies to dates, factors and specialized lists such as dataframes. You can include lists as index-able items in a matrix, but as @thelatemail's comment points out this will be somewhat clunky.

> C <- matrix(0, 3,2)
> W <- matrix(1, 4,5)
> S <- matrix(2, 6,7)
> bigM <- matrix( list(), 2, 3)
> bigM[1,1] <- list(C)
> bigM[2,2] <- list(W)
> bigM[2,3] <- list(S)
> bigM
     [,1]      [,2]       [,3]      
[1,] Numeric,6 NULL       NULL      
[2,] NULL      Numeric,20 Numeric,42
> bigM[2,3][[1]][42]
[1] 2

Notice the need to extract the matrix itself with [[1]] after extracting it as a list with [2,3]. It's only after that additonal step thay you can get the 42nd item in the matrix, whould alos have been the [6,7]th item if you chose to reference it by row,column indices.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • This looks unnecessary complicated. OP should simply use an array. – Roland Apr 08 '14 at 07:13
  • Your suggestion fails to address the fact that both arrays and matrices will strip attributes. Try putting a date-time object or a dataframe into an array. You will either destroy the array structure and end up with an R list or loose the attributes. – IRTFM Apr 09 '14 at 01:48