# loop over matrix: subscript out of bounds

 Classic List Threaded
14 messages
Reply | Threaded
Open this post in threaded view
|

## loop over matrix: subscript out of bounds

 I have a basic for loop with a simple matrix. The code is doing what it is supposed to do, but I'm still wondering the error "subscript out of bounds". What would be a smoother way to code such a basic for loop? myMatrix <- matrix(0,5,12) for(i in 1:nrow(myMatrix)) {   for(i in 1:ncol(myMatrix)) {     myMatrix[i,i] = -1     myMatrix[i,i+1] = 1 }} print(myMatrix) Thanks in advance!         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Both loops are on 'i', which is a bad idea. :-) Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix) On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi <[hidden email]> wrote: > I have a basic for loop with a simple matrix. The code is doing what it is > supposed to do, but I'm still wondering the error "subscript out of > bounds". What would be a smoother way to code such a basic for loop? > > myMatrix <- matrix(0,5,12) > for(i in 1:nrow(myMatrix)) { >   for(i in 1:ncol(myMatrix)) { >     myMatrix[i,i] = -1 >     myMatrix[i,i+1] = 1 > }} > print(myMatrix) > > Thanks in advance! > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/> posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Hello, Eric is right but... You have two assignments. The second sets a value that will be overwritten is the next iteration by myMatrix[i,i] = -1 when 'i' becomes the next value. If you fix the second index and use 'j', you might as well do myMatrix[] = -1 myMatrix[, ncol(myMatrix)] = 1 Hope this helps, Rui Barradas Às 10:24 de 06/08/2018, Eric Berger escreveu: > Both loops are on 'i', which is a bad idea. :-) > Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix) > > > On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi <[hidden email]> > wrote: > >> I have a basic for loop with a simple matrix. The code is doing what it is >> supposed to do, but I'm still wondering the error "subscript out of >> bounds". What would be a smoother way to code such a basic for loop? >> >> myMatrix <- matrix(0,5,12) >> for(i in 1:nrow(myMatrix)) { >>    for(i in 1:ncol(myMatrix)) { >>      myMatrix[i,i] = -1 >>      myMatrix[i,i+1] = 1 >> }} >> print(myMatrix) >> >> Thanks in advance! >> >>          [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help>> PLEASE do read the posting guide http://www.R-project.org/>> posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 In reply to this post by Maija Sirkjärvi Quoting Maija Sirkjärvi <[hidden email]>: > I have a basic for loop with a simple matrix. The code is doing what it is > supposed to do, but I'm still wondering the error "subscript out of > bounds". What would be a smoother way to code such a basic for loop? > > myMatrix <- matrix(0,5,12) > for(i in 1:nrow(myMatrix)) { >   for(i in 1:ncol(myMatrix)) { >     myMatrix[i,i] = -1 >     myMatrix[i,i+1] = 1 > }} > print(myMatrix) > > Thanks in advance! > Perhaps you do not need loops at all?      myMatrix <- matrix(0, 5, 12)      diag(myMatrix) <- -1      diag(myMatrix[, -1]) <- 1 -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 In reply to this post by Rui Barradas Thanks for help! However, changing the index from i to j for the column vector changes the output. I would like the matrix to be the following: -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 ..... etc. How to code it? Best, Maija >> myMatrix <- matrix(0,5,12) >> for(i in 1:nrow(myMatrix)) { >>    for(i in 1:ncol(myMatrix)) { >>      myMatrix[i,i] = -1 >>      myMatrix[i,i+1] = 1 >> }} >> print(myMatrix) ma 6. elok. 2018 klo 13.58 Rui Barradas ([hidden email]) kirjoitti: > Hello, > > Eric is right but... > > You have two assignments. The second sets a value that will be > overwritten is the next iteration by myMatrix[i,i] = -1 when 'i' becomes > the next value. > > If you fix the second index and use 'j', you might as well do > > myMatrix[] = -1 > myMatrix[, ncol(myMatrix)] = 1 > > Hope this helps, > > Rui Barradas > > Às 10:24 de 06/08/2018, Eric Berger escreveu: > > Both loops are on 'i', which is a bad idea. :-) > > Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix) > > > > > > On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi < > [hidden email]> > > wrote: > > > >> I have a basic for loop with a simple matrix. The code is doing what it > is > >> supposed to do, but I'm still wondering the error "subscript out of > >> bounds". What would be a smoother way to code such a basic for loop? > >> > >> myMatrix <- matrix(0,5,12) > >> for(i in 1:nrow(myMatrix)) { > >>    for(i in 1:ncol(myMatrix)) { > >>      myMatrix[i,i] = -1 > >>      myMatrix[i,i+1] = 1 > >> }} > >> print(myMatrix) > >> > >> Thanks in advance! > >> > >>          [[alternative HTML version deleted]] > >> > >> ______________________________________________ > >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see > >> https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/> >> posting-guide.html > >> and provide commented, minimal, self-contained, reproducible code. > >> > > > >       [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 > Thanks for help! > However, changing the index from i to j for the column vector changes the > output. I would like the matrix to be the following: > -1 1 0 0 0 0 0 > 0 -1 1 0 0 0 0 > 0 0 -1 1 0 0 0 > ..... > etc. > How to code it? as Enrico Schumann showed you:  Without any loop, a very nice R-ish way (see his message)! Martin > Best, > Maija ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Thanks, but I didn't quite get it. And I don't get it running as it should. ti 7. elok. 2018 klo 10.47 Martin Maechler ([hidden email]) kirjoitti: > > > Thanks for help! > > However, changing the index from i to j for the column vector changes the > > output. I would like the matrix to be the following: > > > -1 1 0 0 0 0 0 > > 0 -1 1 0 0 0 0 > > 0 0 -1 1 0 0 0 > > ..... > > etc. > > > How to code it? > > as Enrico Schumann showed you:  Without any loop, a very nice > R-ish way (see his message)! > > Martin > > > Best, > > Maija > >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Hello, If it is not running as you want it, you should say what went wrong. Post the code that you have tried and the expected output, please. (In fact, the lack of expected output was the reason why my suggestion was completely off target.) Rui Barradas On 07/08/2018 09:20, Maija Sirkjärvi wrote: > Thanks, but I didn't quite get it. And I don't get it running as it should. > > ti 7. elok. 2018 klo 10.47 Martin Maechler ([hidden email] > ) kirjoitti: > > >      > Thanks for help! >      > However, changing the index from i to j for the column vector >     changes the >      > output. I would like the matrix to be the following: > >      > -1 1 0 0 0 0 0 >      > 0 -1 1 0 0 0 0 >      > 0 0 -1 1 0 0 0 >      > ..... >      > etc. > >      > How to code it? > >     as Enrico Schumann showed you:  Without any loop, a very nice >     R-ish way (see his message)! > >     Martin > >      > Best, >      > Maija > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Thanks! If I do it like this: myMatrix <- matrix(0,5,5*2-3) print(myMatrix) for(i in 2:nrow(myMatrix))   for(j in 2:ncol(myMatrix))     myMatrix[i-1,j-1] = -1     myMatrix[i-1,j] = 1 print(myMatrix) I get the following result:    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,]   -1   -1   -1   -1   -1   -1    0 [2,]   -1   -1   -1   -1   -1   -1    0 [3,]   -1   -1   -1   -1   -1   -1    0 [4,]   -1   -1   -1   -1   -1   -1    1 [5,]    0    0    0    0    0    0    0 However. The result that I would need to get would be like this:    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,]   -1   1   0   0   0   0    0 [2,]   0   -1   1   0   0   0    0 [3,]   0   0   -1   1   0   0    0 [4,]   0   0   0   -1   1   0    0 [5,]    0    0    0    0    -1  1    0 I'd rather not create symmetric matrices as I would really like to learn how to do this thing "the hard way" as I find matrix iteration to be quite a basic procedure in everything I'm trying to do. Thanks again! Maija ti 7. elok. 2018 klo 17.37 Rui Barradas ([hidden email]) kirjoitti: > Hello, > > If it is not running as you want it, you should say what went wrong. > Post the code that you have tried and the expected output, please. > (In fact, the lack of expected output was the reason why my suggestion > was completely off target.) > > Rui Barradas > > On 07/08/2018 09:20, Maija Sirkjärvi wrote: > > Thanks, but I didn't quite get it. And I don't get it running as it > should. > > > > ti 7. elok. 2018 klo 10.47 Martin Maechler ([hidden email] > > ) kirjoitti: > > > > > >      > Thanks for help! > >      > However, changing the index from i to j for the column vector > >     changes the > >      > output. I would like the matrix to be the following: > > > >      > -1 1 0 0 0 0 0 > >      > 0 -1 1 0 0 0 0 > >      > 0 0 -1 1 0 0 0 > >      > ..... > >      > etc. > > > >      > How to code it? > > > >     as Enrico Schumann showed you:  Without any loop, a very nice > >     R-ish way (see his message)! > > > >     Martin > > > >      > Best, > >      > Maija > > >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 You only need one "for loop" for(i in 2:nrow(myMatrix)) {    myMatrix[i-1,i-1] = -1    myMatrix[i-1,i] = 1 } HTH, Eric On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi <[hidden email]> wrote: > Thanks! > > If I do it like this: > > myMatrix <- matrix(0,5,5*2-3) > print(myMatrix) > for(i in 2:nrow(myMatrix)) >   for(j in 2:ncol(myMatrix)) >     myMatrix[i-1,j-1] = -1 >     myMatrix[i-1,j] = 1 > print(myMatrix) > > I get the following result: > >    [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,]   -1   -1   -1   -1   -1   -1    0 > [2,]   -1   -1   -1   -1   -1   -1    0 > [3,]   -1   -1   -1   -1   -1   -1    0 > [4,]   -1   -1   -1   -1   -1   -1    1 > [5,]    0    0    0    0    0    0    0 > > However. The result that I would need to get would be like this: > >    [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,]   -1   1   0   0   0   0    0 > [2,]   0   -1   1   0   0   0    0 > [3,]   0   0   -1   1   0   0    0 > [4,]   0   0   0   -1   1   0    0 > [5,]    0    0    0    0    -1  1    0 > > I'd rather not create symmetric matrices as I would really like to learn > how to do this thing "the hard way" as I find matrix iteration to be quite > a basic procedure in everything I'm trying to do. > > Thanks again! > Maija > > > > > ti 7. elok. 2018 klo 17.37 Rui Barradas ([hidden email]) kirjoitti: > > > Hello, > > > > If it is not running as you want it, you should say what went wrong. > > Post the code that you have tried and the expected output, please. > > (In fact, the lack of expected output was the reason why my suggestion > > was completely off target.) > > > > Rui Barradas > > > > On 07/08/2018 09:20, Maija Sirkjärvi wrote: > > > Thanks, but I didn't quite get it. And I don't get it running as it > > should. > > > > > > ti 7. elok. 2018 klo 10.47 Martin Maechler ([hidden email] > > > ) kirjoitti: > > > > > > > > >      > Thanks for help! > > >      > However, changing the index from i to j for the column vector > > >     changes the > > >      > output. I would like the matrix to be the following: > > > > > >      > -1 1 0 0 0 0 0 > > >      > 0 -1 1 0 0 0 0 > > >      > 0 0 -1 1 0 0 0 > > >      > ..... > > >      > etc. > > > > > >      > How to code it? > > > > > >     as Enrico Schumann showed you:  Without any loop, a very nice > > >     R-ish way (see his message)! > > > > > >     Martin > > > > > >      > Best, > > >      > Maija > > > > > > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/> posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Thanks a lot ! That's it! Maija ke 8. elok. 2018 klo 12.53 Eric Berger ([hidden email]) kirjoitti: > You only need one "for loop" > > for(i in 2:nrow(myMatrix)) { >    myMatrix[i-1,i-1] = -1 >    myMatrix[i-1,i] = 1 > } > > HTH, > Eric > > > On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi < > [hidden email]> wrote: > >> Thanks! >> >> If I do it like this: >> >> myMatrix <- matrix(0,5,5*2-3) >> print(myMatrix) >> for(i in 2:nrow(myMatrix)) >>   for(j in 2:ncol(myMatrix)) >>     myMatrix[i-1,j-1] = -1 >>     myMatrix[i-1,j] = 1 >> print(myMatrix) >> >> I get the following result: >> >>    [,1] [,2] [,3] [,4] [,5] [,6] [,7] >> [1,]   -1   -1   -1   -1   -1   -1    0 >> [2,]   -1   -1   -1   -1   -1   -1    0 >> [3,]   -1   -1   -1   -1   -1   -1    0 >> [4,]   -1   -1   -1   -1   -1   -1    1 >> [5,]    0    0    0    0    0    0    0 >> >> However. The result that I would need to get would be like this: >> >>    [,1] [,2] [,3] [,4] [,5] [,6] [,7] >> [1,]   -1   1   0   0   0   0    0 >> [2,]   0   -1   1   0   0   0    0 >> [3,]   0   0   -1   1   0   0    0 >> [4,]   0   0   0   -1   1   0    0 >> [5,]    0    0    0    0    -1  1    0 >> >> I'd rather not create symmetric matrices as I would really like to learn >> how to do this thing "the hard way" as I find matrix iteration to be quite >> a basic procedure in everything I'm trying to do. >> >> Thanks again! >> Maija >> >> >> >> >> ti 7. elok. 2018 klo 17.37 Rui Barradas ([hidden email]) kirjoitti: >> >> > Hello, >> > >> > If it is not running as you want it, you should say what went wrong. >> > Post the code that you have tried and the expected output, please. >> > (In fact, the lack of expected output was the reason why my suggestion >> > was completely off target.) >> > >> > Rui Barradas >> > >> > On 07/08/2018 09:20, Maija Sirkjärvi wrote: >> > > Thanks, but I didn't quite get it. And I don't get it running as it >> > should. >> > > >> > > ti 7. elok. 2018 klo 10.47 Martin Maechler ( >> [hidden email] >> > > ) kirjoitti: >> > > >> > > >> > >      > Thanks for help! >> > >      > However, changing the index from i to j for the column vector >> > >     changes the >> > >      > output. I would like the matrix to be the following: >> > > >> > >      > -1 1 0 0 0 0 0 >> > >      > 0 -1 1 0 0 0 0 >> > >      > 0 0 -1 1 0 0 0 >> > >      > ..... >> > >      > etc. >> > > >> > >      > How to code it? >> > > >> > >     as Enrico Schumann showed you:  Without any loop, a very nice >> > >     R-ish way (see his message)! >> > > >> > >     Martin >> > > >> > >      > Best, >> > >      > Maija >> > > >> > >> >>         [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help>> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html>> and provide commented, minimal, self-contained, reproducible code. >> > >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 In reply to this post by Eric Berger >>>>> Eric Berger    on Wed, 8 Aug 2018 12:53:32 +0300 writes: > You only need one "for loop" > for(i in 2:nrow(myMatrix)) { >    myMatrix[i-1,i-1] = -1 >    myMatrix[i-1,i] = 1 > } > > HTH, > Eric and why are you not using Enrico Schumann's even nicer solution (from August 6) that I had mentioned too ? Here's the link to it in the (official) R-help archives:  https://stat.ethz.ch/pipermail/r-help/2018-August/455673.htmlMaija said > Thanks, but I didn't quite get it. And I don't get it running as it should. and actually she is right that that version does not work for all dimensions of 'myMatrix' -- it does need  ncol(.) >= 3 but neither does the above solution -- it only works for nrow(.) >= 2 Here's a function version of Enrico's that does work in all cases(!) without a for loop -- including examples (as comments) mkMat <- function(n=5, m=7) {     M <- matrix(0, n,m)     diag(M) <- -1     ## this fails when m == ncol(M) <= 2, and ', drop=FALSE' does *not* help :     ## diag(M[, -1]) <- 1     ## diag(M[, -1, drop=FALSE]) <- 1     ## This *does* work:     M[col(M) - row(M) == 1L] <- 1     M } mkMat() ##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] ## [1,]   -1    1    0    0    0    0    0 ## [2,]    0   -1    1    0    0    0    0 ## [3,]    0    0   -1    1    0    0    0 ## [4,]    0    0    0   -1    1    0    0 ## [5,]    0    0    0    0   -1    1    0 mkMat(3,5) ##      [,1] [,2] [,3] [,4] [,5] ## [1,]   -1    1    0    0    0 ## [2,]    0   -1    1    0    0 ## [3,]    0    0   -1    1    0 mkMat(5,3) ##      [,1] [,2] [,3] ## [1,]   -1    1    0 ## [2,]    0   -1    1 ## [3,]    0    0   -1 ## [4,]    0    0    0 ## [5,]    0    0    0 ## Show that all small (m,n) work: for(m in 0:3)     for(n in 0:3) {         cat(sprintf("(%d,%d):\n", n,m)); print(mkMat(n,m))     } ## (output not shown here) > > On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi <[hidden email]> > wrote: > > >   [.............] > >   [.............] > > However. The result that I would need to get would be like this: > > > >     [,1] [,2] [,3] [,4] [,5] [,6] [,7] > > [1,]  -1    1    0    0    0    0    0 > > [2,]   0   -1    1    0    0    0    0 > > [3,]   0    0   -1    1    0    0    0 > > [4,]   0    0    0   -1    1    0    0 > > [5,]   0    0    0    0   -1    1    0 ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 > >>>>> Eric Berger    on Wed, 8 Aug 2018 12:53:32 +0300 writes: > > > You only need one "for loop" > > for(i in 2:nrow(myMatrix)) { > >    myMatrix[i-1,i-1] = -1 > >    myMatrix[i-1,i] = 1 > > } Or none, with matrix-based array indexing and explicit control of the indices to prevent overrun in : mkMat <- function(n=5, m=7) {    M <- matrix(0, n,m)    i <- 1:min(n,m)    j <- i[i
Reply | Threaded
Open this post in threaded view
|

## Re: loop over matrix: subscript out of bounds

 Hello, There are now three solutions to the OP's problem. I have timed them and the results depend on the matrix size. The solution I thought would be better, Enrico's diag(), is in fact the slowest. As for the other two, Eric's for loop is 50% fastest than the matrix index for small matrices but its relative performance degrades as the matrix becomes bigger and bigger. library(microbenchmark) #Enrico Schumann mkMat_diag <- function(nr = 5, nc = 7) {      M <- matrix(0, nr, nc)      diag(M) <- -1      diag(M[, -1]) <- 1      M } #Eric Berger mkMat_loop <- function(nr = 5, nc = 7) {      M <- matrix(0, nr, nc)      for(i in 2:nrow(M)) {         M[i - 1, i - 1] <- -1         M[i - 1, i] <- 1      }      M } #S.Ellison mkMat_index <- function(nr = 5, nc = 7) {     M <- matrix(0, nr, nc)     i <- 1:min(nr, nc)     j <- i[i < nc]     M[ cbind(i, i) ] <- -1     M[ cbind(j, j + 1) ] <- 1     M } microbenchmark(      loop = mkMat_loop(),      index = mkMat_index(),      diag = mkMat_diag(),      times = 1e3 ) microbenchmark(      loop = mkMat_loop(50, 70),      index = mkMat_index(50, 70),      diag = mkMat_diag(50, 70) ) microbenchmark(      loop = mkMat_loop(500, 700),      index = mkMat_index(500, 700),      diag = mkMat_diag(500, 700) ) Hope this helps, Rui Barradas On 08/08/2018 12:59, S Ellison wrote: > >>>>>>> Eric Berger    on Wed, 8 Aug 2018 12:53:32 +0300 writes: >> >>> You only need one "for loop" >>> for(i in 2:nrow(myMatrix)) { >>>     myMatrix[i-1,i-1] = -1 >>>     myMatrix[i-1,i] = 1 >>> } > > Or none, with matrix-based array indexing and explicit control of the indices to prevent overrun in : > > mkMat <- function(n=5, m=7) { >     M <- matrix(0, n,m) >     i <- 1:min(n,m) >     j <- i[i     M[ cbind(i,i) ] <- -1 >     M[ cbind(j, j+1) ] <- 1 >     M > } > > > > > ******************************************************************* > This email and any attachments are confidential. Any use...{{dropped:8}} > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.