How to reduce its execution time?

5 ビュー (過去 30 日間)
Sadiq Akbar
Sadiq Akbar 2022 年 12 月 26 日
コメント済み: Sadiq Akbar 2022 年 12 月 29 日
I have the following piece of code. It works but takes time. How can we reduce its time to a very minimum value?
u=[1 3 5 7 20 30 40 50];
b=u;
Noise=5;
[R,C]=size(b);
P=C/2;
M=2*C;
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate xo
%%%%%%%%%%%%%%%%%%%%%%%%%%
xo=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cosd(u(P+i)));
end
end
xo=awgn(xo,Noise);% add Noise
%%%%%%%%%%%%%%%%%%%%%
% Calculate xe
%%%%%%%%%%%%%%%%%%%%%
xe=zeros(1,M);
for k=1:M
for i=1:P
xe(1,k)=xe(1,k)+b(i)*exp(-1i*(k-1)*pi*cosd(b(P+i)));
end
end
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
abc=0.0;
for m1=1:M
abc=abc+(abs(xo(1,m1)-xe(1,m1))).^2;
end
abc=abc/M;
e=abc

採用された回答

Voss
Voss 2022 年 12 月 26 日
編集済み: Voss 2022 年 12 月 26 日
u=[1 3 5 7 20 30 40 50];
b=u;
Noise=5;
[R,C]=size(b);
P=C/2;
M=2*C;
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
% not sure why you sort b and u, since you have b=u above. seems like you
% can just sort one of them. anyway, I don't know what the "swapping vector
% b" code is supposed to do, so I left it alone. see below for the rest of
% it:
%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate xo
%%%%%%%%%%%%%%%%%%%%%%%%%%
% xo=zeros(1,M);
% for k=1:M
% for i=1:P
% xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cosd(u(P+i)));
% end
% end
xo = sum(u(1:P).*exp(-1i.*(0:M-1).'.*pi.*cosd(u(P+1:C))),2).';
xo=awgn(xo,Noise);% add Noise
%%%%%%%%%%%%%%%%%%%%%
% Calculate xe
%%%%%%%%%%%%%%%%%%%%%
% xe=zeros(1,M);
% for k=1:M
% for i=1:P
% xe(1,k)=xe(1,k)+b(i)*exp(-1i*(k-1)*pi*cosd(b(P+i)));
% end
% end
xe = sum(b(1:P).*exp(-1i.*(0:M-1).'.*pi.*cosd(b(P+1:C))),2).';
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
% abc=0.0;
% for m1=1:M
% abc=abc+(abs(xo(1,m1)-xe(1,m1))).^2;
% end
% abc=abc/M
e = mean(abs(xo-xe).^2,2);
  1 件のコメント
Sadiq Akbar
Sadiq Akbar 2022 年 12 月 29 日
Thanks a lot to both of you dear Karim and Voss.

サインインしてコメントする。

その他の回答 (1 件)

Karim
Karim 2022 年 12 月 26 日
編集済み: Karim 2022 年 12 月 26 日
Hi, I vectorized your code. Using tic/toc procedure the run time is about 0.04 seconds.
Hope it helps.
tic
% transpose u and b
% we want these to be column vectors for easy vectorization
u = [1 3 5 7 20 30 40 50]';
b = u;
Noise = 5;
[R,~] = size(b);
P = R/2;
M = 2*R;
I commented the lines below, since they don't do anything. You start with u=b and you end up with u=b. Hence these steps do nothing, you will need to provide details on what you mean with "swapping b"
% % Swapping vector b
% [~, ix] = sort(u); % u is my desired vector
% [~, ix1(ix)] = sort(b);
% b = b(ix1);
% calculate xo
u1 = u( 1:P );
u2 = u( P+(1:P));
k = 1:M;
xo = sum( u1.*exp( -1i.*pi*cosd(u2).*(k-1) ) , 1);
% add Noise
xo = awgn(xo,Noise);
% Calculate xe
b1 = b( 1:P );
b2 = b( P+(1:P));
xe = sum( b1.*exp( -1i.*pi*cosd(b2).*(k-1) ) , 1);
% MSE
e = mean( abs( xo(1,:) - xe(1,:) ) .^2 )
e = 0.2862
toc
Elapsed time is 0.040369 seconds.
  2 件のコメント
Voss
Voss 2022 年 12 月 26 日
Don't forget about this:
abc=abc/M;
Karim
Karim 2022 年 12 月 26 日
yes indeed, thank you for pointing it out!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by