Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list of matrix in R

Tags:

list

r

matrix

I want to create a list of 2D matrices

> x
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

> y
     [,1] [,2]
[1,]  301  306
[2,]  302  307
[3,]  303  308
[4,]  304  309
[5,]  305  310

> MATS<-c(x,y)

> MATS[1]
[1] 1

I would like to be able to refer to MATS[1] as if it where x...

like image 228
ManInMoon Avatar asked Dec 07 '12 14:12

ManInMoon


People also ask

How to convert a list to matrix in R?

If we have a list that contain vectors having even number of elements in total then we can create a matrix of those elements. example, if a list contain 8 vectors and the total number of elements in those 8 vectors is 100 or any other multiple of 2 then we can create a matrix of those elements.

How to create a matrix of a list of vectors?

If we have a list that contain vectors having even number of elements in total then we can create a matrix of those elements. example, if a list contain 8 vectors and the total number of elements in those 8 vectors is 100 or any other multiple of 2 then we can create a matrix of those elements. This can be done by using unlist function inside ...

How many rows are in a matrix in R?

Table 1 shows the structure of our first example matrix: It contains five rows and three integer columns. The output of the previous R syntax is shown in Table 2: Another matrix containing character letters. This example demonstrates how to combine multiple matrix objects in a single list.

What are matrices in MATLAB?

Matrices are nothing more than a collection of data elements arranged in a rectangular layout that is two-dimensional. An example matrix with 3x3 dimensions looks like this. The most important thing you need to remember to get started with matrices is the matrix () function.


1 Answers

Try

x <- matrix(1:10, ncol=2)
y <- x+300

MATS <- list(x, y) # use 'list' instead of 'c' to create a list of matrices
MATS
[[1]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

[[2]]
     [,1] [,2]
[1,]  301  306
[2,]  302  307
[3,]  303  308
[4,]  304  309
[5,]  305  310

Here you have to refer to MATS[[1]] as if it were x

If you want to append a new matrix to the exiting list try

z <- x+500
MATS[[3]] <- z  # appeding a new matrix to the existing list
MATS

[[1]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

[[2]]
     [,1] [,2]
[1,]  301  306
[2,]  302  307
[3,]  303  308
[4,]  304  309
[5,]  305  310

[[3]]
     [,1] [,2]
[1,]  501  506
[2,]  502  507
[3,]  503  508
[4,]  504  509
[5,]  505  510

One drawback of this approach is that you have to know the position in the list where you have to append the new matrix, if you don't know it or simply if you dont want this approach, then here's a trick:

unlist(list(MATS, list(z)), recursive=FALSE) # will give u the same list :D
like image 198
Jilber Urbina Avatar answered Oct 15 '22 22:10

Jilber Urbina