Phoebe Tyson
Phoebe Tyson

Reputation: 1

Incorrect number of subscripts on matrix?

I have a list filled with 5x5 matrices and am trying to convert each matrix in the list to a vector using a for loop as follows,

listT<-list()
number <- 0

for(a in 0:3){
  for(i in 1:(5-a)){
    for(j in 1:(5-a)){
      A<-matrix(0,nrow=5,ncol=5)
      A[c(i:(i+a)),c(j:(j+a))]<-1
      number<-number+1
      listT[[number]]<-A

       }

   }

}


vectors<-matrix(0,25,54)
for (number in 1:54){
  vectors[,number] <- t(as.vector(listT[[number]]))
}

But it is coming up with the error message "Error in vectors[,number] <- t(as.vector(listT[[number]])) : incorrect number of subscripts on matrix" I want to get an output of 54 vectors of length 25.

Upvotes: 0

Views: 212

Answers (2)

Cole
Cole

Reputation: 11255

Instead of your loop at the end, sapply() loops through each element of the list apply a function to the list:

sapply(listT, as.vector)

And here is how I would refactor the complete code:

n_by_n <- 3
A <- matrix(0, nrow = n_by_n, ncol = n_by_n)

lst <- unlist(
  lapply(seq_len(n_by_n -1) -1, 
         function(a) {
           all_combos <- expand.grid(1:(n_by_n-a), 1:(n_by_n-a))
           lapply(as.data.frame(t(all_combos)),
                  function(x) {
                    A[seq(x[1], x[1] + a), seq(x[2], x[2] + a)] <- 1
                    return(A)
                    }
                  )
       }),
  use.names = F,
  recursive = F)

sapply(lst, as.vector)

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    1    0    0    0    0    0    0    0    0     1     0     0     0
 [2,]    0    1    0    0    0    0    0    0    0     1     1     0     0
 [3,]    0    0    1    0    0    0    0    0    0     0     1     0     0
 [4,]    0    0    0    1    0    0    0    0    0     1     0     1     0
 [5,]    0    0    0    0    1    0    0    0    0     1     1     1     1
 [6,]    0    0    0    0    0    1    0    0    0     0     1     0     1
 [7,]    0    0    0    0    0    0    1    0    0     0     0     1     0
 [8,]    0    0    0    0    0    0    0    1    0     0     0     1     1
 [9,]    0    0    0    0    0    0    0    0    1     0     0     0     1

Upvotes: 0

Bill Chen
Bill Chen

Reputation: 1749

I did add one line in the code, and I didn't have error. See code:

listT <- list()
number = 0 . # --------------------------- I added this line.
for(a in 0:3){
  for(i in 1:(5-a)){
    for(j in 1:(5-a)){
      A<-matrix(0,nrow=5,ncol=5)
      A[c(i:(i+a)),c(j:(j+a))] <- 1
      number <- number+1
      listT[[number]]<-A

       }

   }

}

vectors<-matrix(0,25,54)
for (number in 1:54){
  vectors[,number] <- t(as.vector(listT[[number]]))
}

The output is here:

> vectors
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
 [1,]    1    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [2,]    0    0    0    0    0    1    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [3,]    0    0    0    0    0    0    0    0    0     0     1     0     0     0     0     0     0     0     0     0     0     0     0     0
 [4,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0
 [5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     1     0     0     0
 [6,]    0    1    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [7,]    0    0    0    0    0    0    1    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 [8,]    0    0    0    0    0    0    0    0    0     0     0     1     0     0     0     0     0     0     0     0     0     0     0     0
 [9,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0
[10,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0
[11,]    0    0    1    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[12,]    0    0    0    0    0    0    0    1    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[13,]    0    0    0    0    0    0    0    0    0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0
[14,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0
[15,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0
[16,]    0    0    0    1    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[17,]    0    0    0    0    0    0    0    0    1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[18,]    0    0    0    0    0    0    0    0    0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0
      [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47]
 [1,]     0     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0     0
 [2,]     0     1     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0     1     0     0     1     0     0
 [3,]     0     0     0     0     0     1     0     0     0     1     0     0     0     0     0     0     0     1     0     0     1     0     0
 [4,]     0     0     0     0     0     0     0     0     0     1     0     0     0     1     0     0     0     0     0     0     1     0     0
 [5,]     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0
 [6,]     0     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     0     0     0     0
 [7,]     0     1     1     0     0     1     1     0     0     0     0     0     0     0     0     0     0     1     1     0     1     1     0
 [8,]     0     0     0     0     0     1     1     0     0     1     1     0     0     0     0     0     0     1     1     0     1     1     0
 [9,]     0     0     0     0     0     0     0     0     0     1     1     0     0     1     1     0     0     0     0     0     1     1     0
[10,]     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     0     0     0     0     0     0     0     0
[11,]     0     0     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     0     0     0
[12,]     0     0     1     1     0     0     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
[13,]     0     0     0     0     0     0     1     1     0     0     1     1     0     0     0     0     0     1     1     1     1     1     1
[14,]     0     0     0     0     0     0     0     0     0     0     1     1     0     0     1     1     0     0     0     0     1     1     1
[15,]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     0     0     0     0     0     0     0
[16,]     0     0     0     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     0     0     0
[17,]     0     0     0     1     1     0     0     1     1     0     0     0     0     0     0     0     0     0     1     1     0     1     1
[18,]     0     0     0     0     0     0     0     1     1     0     0     1     1     0     0     0     0     0     1     1     0     1     1
      [,48] [,49] [,50] [,51] [,52] [,53] [,54]
 [1,]     0     0     0     1     0     0     0
 [2,]     0     0     0     1     0     1     0
 [3,]     1     0     0     1     0     1     0
 [4,]     1     0     0     1     0     1     0
 [5,]     1     0     0     0     0     1     0
 [6,]     0     0     0     1     1     0     0
 [7,]     0     0     0     1     1     1     1
 [8,]     1     1     0     1     1     1     1
 [9,]     1     1     0     1     1     1     1
[10,]     1     1     0     0     0     1     1
[11,]     0     0     0     1     1     0     0
[12,]     0     0     0     1     1     1     1
[13,]     1     1     1     1     1     1     1
[14,]     1     1     1     1     1     1     1
[15,]     1     1     1     0     0     1     1
[16,]     0     0     0     1     1     0     0
[17,]     0     0     0     1     1     1     1
[18,]     0     1     1     1     1     1     1
 [ reached getOption("max.print") -- omitted 7 rows ]

Upvotes: 1

Related Questions