Speeding up code
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2011-7-25
Do you have a question?
We cannot run your function, because you do not provide [nPeople], [nFactor] and [Atran].
回答(1 个)
Jan
2011-7-25
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Weather and Atmospheric Science 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!