[R] Efficient nested loops

David Winsemius dwinsemius at comcast.net
Thu Oct 21 15:00:31 CEST 2010


On Oct 21, 2010, at 4:40 AM, Francisco Javier Santos Alamillos wrote:

> 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?

Are you sure this is a proper specification of the "task"?. You have  
overwritten the "result" with max(.) multiple times (the product of  
the dimensions) and only have at the end that effort just the max of  
the last slice of the variable.

>
> Thanks in advance!
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list