How to reduce its execution time?

조회 수: 3 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by