The best way for making speciall matrix

5 messages
Open this post in threaded view
|

The best way for making speciall matrix

 Hi my friends, I want to make the below matrix in r: 1 0 0 4 0 2 0 5 0 0 3 6 I used the below code: matrix(c(1,0,0,0,2,0,0,0,3,4,5,6),nrow=3) My code works. But I do not like my solution way. I am thinking to find the simplest way for making this matrix. Do you think my code is the simplest code for making this matrix? If not, could anyone writes a simpler code than my one?         [[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.
Open this post in threaded view
|

Re: The best way for making speciall matrix

 Dear Vahid, Would this help? > row1<- c(1,0,0,4) > row2<- c(0,2,0,5) > row3<- c(0,0,3,6) > mymatrix <- rbind(row1,row2,row3) > mymatrix      [,1] [,2] [,3] [,4] row1    1    0    0    4 row2    0    2    0    5 row3    0    0    3    6 > Best Regards, Ashim On Sat, May 23, 2020 at 3:16 PM Vahid Borji <[hidden email]> wrote: > > Hi my friends, > > I want to make the below matrix in r: > > 1 0 0 4 > > 0 2 0 5 > > 0 0 3 6 > > I used the below code: > > matrix(c(1,0,0,0,2,0,0,0,3,4,5,6),nrow=3) > > My code works. But I do not like my solution way. I am thinking to find the > simplest way for making this matrix. Do you think my code is the simplest > code for making this matrix? If not, could anyone writes a simpler code > than my one? > >         [[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.
Open this post in threaded view
|

Re: The best way for making speciall matrix

 In reply to this post by Vahid Hello, Use diag() and cbind(). special_mat <- function(n){    if(n %% 2 != 0) {      msg <- paste(sQuote(n), 'is not a multiple of 2, will use')      n <- 2*(n%/% 2)      msg <- paste(msg, sQuote(n))      warning(msg)    }    x <- diag(n/2)    diag(x) <- seq.int(n/2)    cbind(x, (n/2 + 1):n) } special_mat(6) special_mat(8) special_mat(7) Hope this helps, Rui Barradas Às 10:45 de 23/05/20, Vahid Borji escreveu: > Hi my friends, > > I want to make the below matrix in r: > > 1 0 0 4 > > 0 2 0 5 > > 0 0 3 6 > > I used the below code: > > matrix(c(1,0,0,0,2,0,0,0,3,4,5,6),nrow=3) > > My code works. But I do not like my solution way. I am thinking to find the > simplest way for making this matrix. Do you think my code is the simplest > code for making this matrix? If not, could anyone writes a simpler code > than my one? > > [[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.