Hi Wolfgang,
how about a loop?
matRandom <- matrix(runif(n=600000), ncol=6)
## variant 1
system.time(test1 <- pmax(matRandom[,1], matRandom[,2], matRandom[,3],
matRandom[,4], matRandom[,5], matRandom[,6]))
User System verstrichen
0.01 0.00 0.01
## variant 2
system.time(test2 <- apply(matRandom, 1, max))
User System verstrichen
0.56 0.00 0.56
## variant 3
system.time({
test3 <- matRandom[ ,1L]
## add a check that ncol(matrix) > 1L
for (i in 2:ncol(matRandom))
test3 <- pmax(test3, matRandom[ ,i])
})
User System verstrichen
0.01 0.00 0.01
> all.equal(test1,test2)
[1] TRUE
> all.equal(test1,test3)
[1] TRUE
Regards,
Enrico
Am 12.10.2011 13:06, schrieb Wolfgang Wu:
I am having the following problem. I want to calculate the maximum of each row
in a matrix. If I pass in the matrix split up by each column then this is no
problem and works great. However I don't know how many columns I have in
advance. In the example below I have 3 columns, but the number of columns is
not fix. So how do I do this?
matRandom<- matrix(runif(n=30), ncol=3);
#Does not work
pmax(matRandom)
#Does work
pmax(matRandom[,1], matRandom[,2], matRandom[,3])
I am aware that I can do it with the apply function, but the calculation is
time sensitive so fast execution is important.
#Apply might be too slow
matRandom<- matrix(runif(n=300000), ncol=3);
system.time(test<- pmax(matRandom[,1], matRandom[,2], matRandom[,3]))
system.time(test<- apply(matRandom, 1, max))
matRandom<- matrix(runif(n=300000), ncol=3);
system.time(test<- pmax(matRandom[,1], matRandom[,2], matRandom[,3]))
user system elapsed
0.02 0.00 0.02
system.time(test<- apply(matRandom, 1, max))
user system elapsed
2.37 0.00 2.38
Thanks for your help.
Regards.
Wolfgang Wu
______________________________________________
R-help@r-project.org mailing list
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.
--
Enrico Schumann
Lucerne, Switzerland
http://nmof.net/
______________________________________________
R-help@r-project.org mailing list
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.