Dear users of S-news:
Two better ways of dealing with the problem have been suggested:
4. rowSums / colSums e.g. colSums(A*A) or rowSums(t(A)^2)
Dos time of function = 0.531s
This was suggested by (in chronological order) Ernst Linder, James Holtman
and Brian D. Ripley.
5. apply e.g. apply(A^2,1,sum)
Dos time of function = 0.54s
This was suggested by Jim Stapleton
Thank you to everyone who replied so quickly. Both methods 4 and 5 are
excellent improvements.
Apologies for the misleading errors in the original message. A corrected
version follows.
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Corrected original text:
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
As part of a function I would like to calculate transpose(A) %*% A so that
the first row of transpose(A) is matched with the 1st column of A. The
second row of Transpose(A) is matched with the second column of A, etc.
For example if:
t(A) = 1 2 3
4 5 6
i.e.
A = 1 4
2 5
3 6
Then the operation would return
transpose(A) %*% A = 14
77
I have used three methods on an example where A is an 18x900 matrix:
1. Apply: e.g. apply(A,2,function(A)t(A)%*%A))
Dos time of function = 4.736s
2. For loop: e.g. for( i in 1: x){test[i]<-t(A[,i])%*%A[,i]}
Dos time of function = 4.837s
3. Matrix multiplication: e.g. diag(t(A)%*%A)
Dos time of function = 1.181s
If I don't do this part of the procedure and use a made-up matrix as a
result for this procedure then the Dos time of the function is 0.531s.
I would like the function to run as quickly as possible to use in
resampling. I have the feeling that there must be a better way to do this
than method 3. If anyone has encountered this problem before I would be
happy to hear if there is a better way.
Splus: S-PLUS® 6.1 for Windows, Professional, Release 1
Operating system: Windows 2000, SP4
Best wishes
Adam Brentnall
a.r.brentnall@maths.soton.ac.uk
Research Student
Dept Mathematics
University of Southampton UK
|