how do I make the for to follow different sequence for the matrix , not ascendent or descendent .

I would like to run the for function with a different sequence rather than in order .
l want the for to print i =3,2,1,6,5,4,7,8,9
is it possible to make this sequence with the for or do I need to use something else.

D=matrix(rnorm(9,10,0.5),nrow = 3,ncol = 3,T)

for (i in 9:1) {

print(i)
}

for (i in 1:9) {

print(i)

}

It will follow the order of the vector you pass.

for (i in c(3,2,1,6,5,4,7,8,9)) {
  print(i)
}
#> [1] 3
#> [1] 2
#> [1] 1
#> [1] 6
#> [1] 5
#> [1] 4
#> [1] 7
#> [1] 8
#> [1] 9

Created on 2022-10-14 with reprex v2.0.2.9000

if I have a matrix and I would like to go thru the matrix in that sequence
(3,2,1,6,5,4,7,8,9), is it possible with for , without place a vector c(3,2,1,6,5,4,7,8,9)
l

You need some way to describe the desired order.

You could shorten it slightly c(3:1,6:4,7:9) because thats the same vector.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.