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]]
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 <vahid.borji65 at gmail.com> 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]] > > ______________________________________________ > 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.
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]]
>
> ______________________________________________
> 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.
>
This sounds like a homework question...
But... numerical linear algebra rocks...
cbind (diag (1:3), 4:6)
On Sat, May 23, 2020 at 9:46 PM Vahid Borji <vahid.borji65 at gmail.com>
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]]
>
> ______________________________________________
> 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.
Hello,
Please keep this on the list, R-help is threaded and it becomes part of
the archives, maybe it will be usefull to others.
You are now asking 2 other different questions.
Are you looking for something like this?
two_values_mat <- function(n, fill = 1, diagonal = 0){
m <- matrix(fill, nrow = n, ncol = n)
diag(m) <- diagonal
m
}
two_values_mat(3)
two_values_mat(3, 0.5, 1)
odd_even_mat <- function(n){
even <- n %% 2 == 0
nr <- floor(sqrt(n)) - !even
nc <- ceiling(n/nr)
i <- c(0, seq.int(n - even))
matrix(2*i + !even, nrow = nr, ncol = nc, byrow = TRUE)
}
odd_even_mat(30)
odd_even_mat(17)
Hope this ehlps,
Rui Barradas
?s 11:52 de 23/05/20, Vahid Borji escreveu:> Thank you Rui,
> Actually, I am making some matrix as follows:
> 0 1 1
> 1 0 1
> 1 1 0
> matrix(rep(c(0,1,1,1),3),nrow=3,ncol=3)
> and
> 1? 3 ? 5? 7? 9? 11
> 13? 15? 17? 19
> 21 ? 23? 25? 27
> 29 31 33 35
> i<-0:17 matrix(2*i+1,nrow=3,byrow=TRUE)
> and
> 1????? 0.5?? 0.5
> 0.5?? ? 1???? 0.5
> 0.5?? 0.5??? 1
> matrix(rep(c(1,0.5,0.5,0.5),3),nrow=3,ncol=3)
>
> For all the above matrixes I have written a code (as you see below each
> matrix) . If you want to make the above matrixes, will you use my codes
> or will you have other simpler ways for making the above matrixes?
>
> On Sat, May 23, 2020 at 2:39 PM Rui Barradas <ruipbarradas at sapo.pt
> <mailto:ruipbarradas at sapo.pt>> wrote:
>
> 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 <http://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]]
> >
> > ______________________________________________
> > R-help at r-project.org <mailto: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.
> >
>