skittles
skittles

Reputation: 13

Creating a list of matrices in R-programming

So I'm trying to create 10 separate 2x2 matrices with only the top left value changing.

I'm fairly new to this, but my best attempt:

x <- 1:10 A<- matrix(c(x,2,4,-1),nrow=2) but this makes 1 big 2-row matrix

How do I fix this?

Upvotes: 1

Views: 2554

Answers (1)

Victorp
Victorp

Reputation: 13856

You can do like this, the result is a list containing 10 matrix :

x <- 1:10
A <- lapply(x, function(x) matrix(c(x,2,4,-1),nrow=2))
A[[1]]
##      [,1] [,2]
## [1,]    1    4
## [2,]    2   -1

A[[2]]
##      [,1] [,2]
## [1,]    2    4
## [2,]    2   -1

Upvotes: 2

Related Questions