Also:
mat+0
#or
mat*1
#However, sapply() based solutions would be slower in large matrices.
#Speed
makeMat3 <- function(x,n){
if(is.numeric(x)){
x <- as.integer(round(x))
x}
stopifnot(is.integer(x))
if(length(x)>=n & max(x)<=n){
indx<-rep(rep(c(1,0),max(length(x),n)),rbind(x,n-x))
Just a further suggestion:
vec <- c(3,2,5,0,1)
mat <- t(sapply(vec,">=",1:max(vec)))
ifelse(mat,1,0)
Cheers,
Christoph
2013/10/11 arun :
> Hi,
>
> In the example you showed:
>
> m1<- matrix(0,length(vec),max(vec))
> 1*!upper.tri(m1)
>
> #or
> m1[!upper.tri(m1)] <- rep(rep(1,length(vec)),vec
Apologies, in case it is double posting (I think my previous message didn't
make it to the list)
Hope the new version of `makeMat3` is bug free:
makeMat3 <- function(x,n){
if(is.numeric(x)){
x <- as.integer(round(x))
x}
stopifnot(is.integer(x))
if(length(x)>=n & max(x)<=n){
indx
Some speed comparison:
set.seed(124)
xtest<- sample(0:9,1e7,replace=TRUE)
system.time({res1 <- makemx(xtest,9)})
# user system elapsed
# 51.124 0.812 52.039
system.time({res2 <- makeMatrix2(xtest,9)})
# user system elapsed
# 3.460 0.168 3.631
identical(res1,res2)
#[1] TRUE
This looks better. My previous solution (makeMatrix2) also did the matrix
indexing without using sapply() route. Replacing the max(x) by n for
non-symmetric matrix:
makeMatrix2<- function(x,n){ #including "n"
if(is.numeric(x)){
x <- as.integer(round(x))
x}
stopifnot(is.integer(x))
m1<- matri
This seems to do it, but as I mentioned in my original post, my
"solution" was tricky. Thinking about it some more, I now realize that
it was too tricky and has the additional flaw of using underlying
representations of objects rather than their exposed interfaces -- i.e
it treats a matrix as a vec
Modified Bert's solution for non-square matrices. For the tested vectors, it
worked. There, could still be some bugs.
x1<- c(3,2,1,4)
x2<- c(2,0,4,3,1)
x3 <- c(2, 1, 2.2)
x4 <- c("a",1,3)
makeMat3 <- function(x,n){
if(is.numeric(x)){
x <- as.integer(round(x))
x}
stopifnot(is.int
Your examples are the problem:
On Fri, Oct 11, 2013 at 2:43 PM, arun wrote:
> Seems like a bug in the code:
> x<- c(3,4,1)
> n<- 3
> matrix(rep(rep(c(1,0),n),rbind(x,n-x)),nc=n,byr=TRUE)
> #Error in rep(rep(c(1, 0), n), rbind(x, n - x)) : invalid 'times' argument
## This can't work since x spec
Seems like a bug in the code:
x<- c(3,4,1)
n<- 3
matrix(rep(rep(c(1,0),n),rbind(x,n-x)),nc=n,byr=TRUE)
#Error in rep(rep(c(1, 0), n), rbind(x, n - x)) : invalid 'times' argument
n<- 4
matrix(rep(rep(c(1,0),n),rbind(x,n-x)),nc=n,byr=TRUE)
#Error in rep(rep(c(1, 0), n), rbind(x, n - x)) : invalid
Thanks Dennis. I noticed I didn't take the "0" value into consideration and
also didn't check the unsorted vector.vec1<- c(2,4,1)
is.numeric(vec1)
#[1] TRUE
makeMat(as.integer(vec1))
makeMatrix2<- function(x){
if(is.numeric(x)){
x <- as.integer(round(x))
x}
stopifnot(is.integer(x))
m1<- matr
simpler (and sloppier) but with **no looping or apply's **
**IFF* the matrix is structured as in the OP's example, then lower.tri
(or upper.tri) should be used:
n <- 4 ## number of columns in matrix -- note that I changed it from
the example; does not have to be square
x <- 1:3 ## the number of
Attempting to follow the OP's conditions and assuming I understood
them correctly, here is one way to wrap this up into a function:
makeMat <- function(x)
{
stopifnot(is.integer(x))
nr <- length(x)
nc <- max(x)
# Initialize a matrix of zeros
m <- matrix(0, nr, nc)
# Condit
Hi,
In the example you showed:
m1<- matrix(0,length(vec),max(vec))
1*!upper.tri(m1)
#or
m1[!upper.tri(m1)] <- rep(rep(1,length(vec)),vec)
#But, in a case like below, perhaps:
vec1<- c(3,4,5)
m2<- matrix(0,length(vec1),max(vec1))
indx <-
cbind(rep(seq_along(vec1),vec1),unlist(tapply(vec1,l
13 matches
Mail list logo