Skip to content
Prev 384052 / 398502 Next

The best way for making speciall matrix

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:
Message-ID: <e90b2dc9-1327-e071-cb6f-10c7fbb1e756@sapo.pt>
In-Reply-To: <CAEPHqhboVGkqyQ1eRaBoJbwO5_YGXiSB6AWQ_a23gZCKAwWsvg@mail.gmail.com>