Speeding up code

I have written a code for matrix multiplication and matrix transpose multiplication for use in LSQR. The time has been a little bad so I did a check and I get 3.6704 for mult and 1.7869 with transpose using the code below.
here A is a 76308x82 double
% Matrix - Vector Multiplication
nf = floor(factorial(82)/(2*factorial(80))+82;
x1 = rand(nf,1);
y = zeros(nPeople,1);
n = floor(nFactor/2);
l = nFactor+1;
tStart = tic;
for a = 1:nPeople
y(a) = A(a,:)*x1(1:nFactor);
end
for i = 1:n
for j = i+1:nFactor
y = y + x1(l)*B(:,i).*B(:,j);
l = l+1;
end
end
tElapsed = toc(tStart);
% Matrix Transpose
x2 = rand(nPeople,1);
n = floor(factorial(82)/(2*factorial(80))+82));
y = zeros(n,1);
tStart = tic;
for a = 1:nFactor
y(a) = Atran(a,:)*x2;
end
l = nFactor+1;
nn = ceil(nFactor/2);
for i = 1:nn
for j = i+1:nFactor
y(l) = y(l) + (B(:,i).*B(:,j))'x2;
end
end
tElapsed = toc(tStart);

1 Comment

Jan
Jan on 25 Jul 2011
Do you have a question?
We cannot run your function, because you do not provide [nPeople], [nFactor] and [Atran].

Sign in to comment.

Answers (1)

Jan
Jan on 25 Jul 2011
MATLAB uses highly optimized BLAS functions for matrix operations. It is strongly recommended to use them, e.g.:
for a = 1:nPeople
y(a) = A(a,:)*x1(1:nFactor);
end
==>
y = x1(1:nFactor) * A(1:nPeople, :);
Or if the dimensions are matching simply "x1 * A".
BTW: "(B(:,i).*B(:,j))'x2" contains a typo.

1 Comment

Ryan
Ryan on 27 Jul 2011
I realize that MATLAB is very optimized. I am sorry that I didn't include the rest however I am new to this forum and was unsure if I could upload it all, but mainly because I do my work remotely and the server was down and the mat file containing A from the code is over 10Mb and I was unsure if I could upload something that large.
In the function I created the other variables are defined by :
load('Data');
A = Data.A;
[nPeople,nFactor] = size(A);
Atran = A';
B = A>0;
The reasons I wrote it that way is because the dimensions
don't match. The B matrix is meant to just show if each person has one of the factors, then do the matrix vector multiplication to get weights for if individuals have both factors.
I did this computation inside with the (B(:,i).*B(:,j)) because I intend to continue to expand out with combinations of factors and I am unsure if Matlab can handle such large matrices. If it can that is great but I still need the function for LSQR because I will changing it later to test a new algorithm.
Does this make sense?

Sign in to comment.

Asked:

on 25 Jul 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!