Speeding up code
2 views (last 30 days)
Show older comments
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
on 25 Jul 2011
Do you have a question?
We cannot run your function, because you do not provide [nPeople], [nFactor] and [Atran].
Answers (1)
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.
See Also
Categories
Find more on Weather and Atmospheric Science in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!