3

I have to put 54 matrices in a list of vector called A. I want the first 25 matrices in this list to be zero matrices except the (i,j)th element of the matrix, which should be equal to one. All my matrices are 5x5. I am using a for loop, but I dont understand how to create the first 25 matrices.

Now I am trying to make 16 matrices of 0 with (i:(i-1),j:(j-1))th element = 1, so that there is a square of 1s of size 2 by 2. For this I have used the following code, but I want to make sure that 'i' is greater that or equal to 2. how can I do this?

    t<-26
for(i in 1:5){
   for(j in 1:5){
     A <- matrix(0, nrow = 5, ncol = 5)
    A[i:(i-1), j:(j-1)] <- 1
    M[[l]] <- A
    t <- t+1
   }
}
A
adiga
  • 34,372
  • 9
  • 61
  • 83
  • What are `i` and `j` (in " (i,j)th element")? Also, what is the question? – kangaroo_cliff Nov 16 '17 at 04:21
  • Something seems to be off in your loop. Are you sure about `for(i in 1:i)` ?? Also go through [this reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so it is easy for others to help you. – Ronak Shah Nov 16 '17 at 04:29
  • @MahakMotwani I think one of the two for loops below is what you are after. – kangaroo_cliff Nov 16 '17 at 04:50
  • Please do not vandalize your posts. Your edit invalidates the comments and answers posted. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) – adiga Nov 16 '17 at 10:25
  • Just use `for(i in 2:5)` instead of `for(i in 1:5)`. I think you have another typo: you want to assign `A` into `M[[t]]`, not `M[[l]]`. – JDL Nov 16 '17 at 10:26

1 Answers1

2

Just a guess as to what you want. Let me know if this is not what you want, then either I can edit or remove it.

A <- vector("list", 54)
mat <- matrix(0, nrow = 5, ncol = 5)

# assign mat to A and the 1 to the ith element of mat (column wise)
for (i in 1:25) {
  A[[i]] <- mat
  A[[i]][i] <- 1  
}

A[[1]]
# [,1] [,2] [,3] [,4] [,5]
# [1,]    1    0    0    0    0
# [2,]    0    0    0    0    0
# [3,]    0    0    0    0    0
# [4,]    0    0    0    0    0
# [5,]    0    0    0    0    0

A[[2]]
# [,1] [,2] [,3] [,4] [,5]
# [1,]    0    0    0    0    0
# [2,]    1    0    0    0    0
# [3,]    0    0    0    0    0
# [4,]    0    0    0    0    0
# [5,]    0    0    0    0    0

A[[25]]
# [,1] [,2] [,3] [,4] [,5]
# [1,]    0    0    0    0    0
# [2,]    0    0    0    0    0
# [3,]    0    0    0    0    0
# [4,]    0    0    0    0    0
# [5,]    0    0    0    0    1

Insert 1 row wise (not the most elegant way to do it)

# assing 1 the ith element of mat (column wise)
for (i in 1:25) {
  mat_1 <- mat
  mat_1[i] <- 1
  A[[i]] <- t(mat_1)
}
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42