[R] Odp: Efficient nested loops

Petr PIKAL petr.pikal at precheza.cz
Thu Oct 21 12:17:46 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 21.10.2010 10:40:40:

> Dear R community,
> 
> I am working with huge arrays, so I spend a lot of time computing. This 
is
> my code:
> 
> for (x in 1:dim(variable)[1]){
>     for (y in 1:dim(variable)[2]){
>         for (z in 1:dim(variable)[3]){
>             result <- max(variable[x,y,z,])
>         }
>     }
> }
> 
> Is there a more efficient procedure to do this task?

is this what you are looking for?

ar<-array(rnorm(24),c(4,3,2))
apply(ar, 3, max)

Regards
Petr


> 
> Thanks in advance!
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list