RE: [R] vectorizing a matrix computation

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Wed 21 Jul 2004 - 01:47:25 EST


I think this should do:

z <- apply(my.matrix, 1:3, scale)
z <- aperm(z, c(2:4, 1))

Andy

> From: Christoph Lehmann
>
> 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
>
> Cheers
>
> Christoph
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jul 21 01:55:43 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:07 EST