[R] vectorizing a matrix computation

Wolski wolski at molgen.mpg.de
Tue Jul 20 11:54:54 CEST 2004

Hallo Christoph!

Using apply and sweep.
Both of them accept  as the second argument (MARGIN) a vector which specifies the dimension.


To specify the subscripts accurately I always need some trials, and to run tests, so I cant you provide with the final solution.

Hope it helps anyway.

Sincerely Eryk.

*********** REPLY SEPARATOR  ***********

On 7/20/2004 at 11:31 AM Christoph Lehmann wrote:

>>>Dear R users
>>>I have a 4-dimensional matrix (actually several 3d (x,y, slices) 
>>>matrices appended over time (volumes))
>>>say, e.g. I want to z-transform the data (subtract the mean and divide 
>>>by the std-deviation)
>>>for (slice in 1:slices) {
>>>     for (x in 1:x.dim) {
>>>         for (y in 1:y.dim) {
>>>         t <- as.matrix(my.matrix[x,y,slice,1:volumes])
>>>         for (vol in 1:volumes) {
>>>             my.matrix.transformed[x,y,slice,vol] <- 
>>>(my.matrix[x,y,slice,vol] - mean(t))/sqrt(var(t))
>>>             }
>>>         }
>>>     }
>>>how can I vectorize such a function using, one of the *apply functions?
>>>many thanks
>>>R-help at stat.math.ethz.ch mailing list
>>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Dipl. bio-chem. Eryk Witold Wolski    @    MPI-Moleculare Genetic   
Ihnestrasse 63-73 14195 Berlin       'v'    
tel: 0049-30-83875219               /   \    
mail: wolski at molgen.mpg.de        ---W-W----    http://www.molgen.mpg.de/~wolski

More information about the R-help mailing list