s-news
[Top] [All Lists]

Matrix row multiplication with a quick dos.time?

To: s-news@wubios.wustl.edu
Subject: Matrix row multiplication with a quick dos.time?
From: Adam Brentnall <a.r.brentnall@maths.soton.ac.uk>
Date: Wed, 25 Feb 2004 16:13:12 +0000
Dear users of S-news

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 row of A. The second row of Transpose(A) is matched with the second column of A, etc.

For example if:

A =     1 2 3
        4 5 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(test)%*%test)

Dos time of function = 1.181s

If I don't do this part of the procedure and use a made-up matrix for A 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

<Prev in Thread] Current Thread [Next in Thread>