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 <maija.sirkjarvi at gmail.com> 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 (ruipbarradas at sapo.pt) 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 (maechler at stat.math.ethz.ch > > > <mailto:maechler at stat.math.ethz.ch>) 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]] > > ______________________________________________ > R-help at r-project.org 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]]
Thanks a lot ! That's it! Maija ke 8. elok. 2018 klo 12.53 Eric Berger (ericjberger at gmail.com) 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 < > maija.sirkjarvi at gmail.com> 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 (ruipbarradas at sapo.pt) 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 ( >> maechler at stat.math.ethz.ch >> > > <mailto:maechler at stat.math.ethz.ch>) 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]] >> >> ______________________________________________ >> R-help at r-project.org 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]]
>>>>> 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, > Ericand 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.html Maija 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 <maija.sirkjarvi at gmail.com> > 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
> >>>>> 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] M[ cbind(i,i) ] <- -1 M[ cbind(j, j+1) ] <- 1 M } ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}