How to reduce its execution time?

5 views (last 30 days)
Sadiq Akbar
Sadiq Akbar on 26 Dec 2022
Commented: Sadiq Akbar on 29 Dec 2022
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

Accepted Answer

Voss
Voss on 26 Dec 2022
Edited: Voss on 26 Dec 2022
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 Comment
Sadiq Akbar
Sadiq Akbar on 29 Dec 2022
Thanks a lot to both of you dear Karim and Voss.

Sign in to comment.

More Answers (1)

Karim
Karim on 26 Dec 2022
Edited: Karim on 26 Dec 2022
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 Comments
Voss
Voss on 26 Dec 2022
Don't forget about this:
abc=abc/M;
Karim
Karim on 26 Dec 2022
yes indeed, thank you for pointing it out!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!