フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How can I make this faster?

2 ビュー (過去 30 日間)
Peter
Peter 2015 年 2 月 5 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I always try not to use for loops but I do not know how to avoid the for loop to do this:
n=1000;
m=100;
A=rand(n,m)+rand(n,m)*1j;
tic
B=zeros(100);
for ii=1:n
B=A(ii,:)'*A(ii,:);
C(ii,:)=B(:).';
end
toc
thanks in advance,
Ignacio

回答 (1 件)

Orion
Orion 2015 年 2 月 5 日
just start by preallocate some memory for C :
clear C C2 B
n=1000;
m=100;
A=rand(n,m)+rand(n,m)*1j;
tic
B=zeros(m);
for ii=1:n
B=A(ii,:)'*A(ii,:);
C(ii,:)=B(:).';
end
toc
tic
C2 = zeros(n,m*m);
for ii=1:n
C2(ii,:) = reshape(A(ii,:)'*A(ii,:),1,m*m);
end
toc
isequal(C,C2)
=>
Elapsed time is 21.619996 seconds. % without preallocation
Elapsed time is 0.464255 seconds. % with preallocation

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by