How to vectorize this piece of code by replacing all the for-loops?

조회 수: 2 (최근 30일)
Sadiq Akbar
Sadiq Akbar 2023년 1월 3일
답변: Voss 2023년 1월 3일
I want to vectorize the following piece of code so that it becomes fast. But M and N can be only coprime numbers and N < M always. I tried but failed badly.
th=pi/180;
u=[40*th 50*th 60*th 70*th];
b=u;
K=length(u);
% Note that N<M coprime No
N=3; M=4;
NE=(N+M-1); % Total
x=0; y=0; z=1; % Initialization
Sto=zeros(NE,K); % matrix initialization
for ii=1:NE
if (x==0);
distance=0;
x=x+1;
elseif (y==0);
distance=z*pi*N;
y=y+1;
else
distance=z*pi*M;
y=0;
z=z+1;
end
for jj=1:K
Sto(ii,jj)=exp(-1i*distance*cos(u(jj))); % matrix equation
end
end
y_act_sum=sum(Sto,2);
y_act_abs=abs(y_act_sum);
xx=(y_act_abs);
x=0;y=0;z=1;
Ste=zeros(NE,K);
for kk=1:NE
if (x==0);
distance=0;
x=x+1;
elseif (y==0);
distance=z*pi*N;
y=y+1;
else
distance=z*pi*M;
y=0;
z=z+1;
end
for i=1:K
Ste(kk,i)=exp(-1i*distance*cos(b(i)));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%%%%%%%
y_est_sum=sum(Ste,2);
y_est_abs=abs(y_est_sum);
yy=(y_est_abs);
correlation=corr(xx,yy);
mse1=zeros(1,NE);
for pp=1:NE
mse1=(xx(pp)-yy(pp));
end
mse2=sum(mse1,2);
mse3=mse2.^2;
mse=mse3/NE;
mse_corr=mse+norm(correlation-1);

채택된 답변

Voss
Voss 2023년 1월 3일
Here is a vectorized code that doesn't have to be modified if you change M or N:
th=pi/180;
u=[40*th 50*th 60*th 70*th];
b=u;
K=length(u);
% Note that N<M coprime No
N=3;
M=4;
NE = N+M-1; % Total
distance = [0; reshape((1:floor(NE/2))*pi.*[N; M],[],1)];
distance(NE+1:end) = [];
Sto = exp(-1i*distance.*cos(u));
Ste = exp(-1i*distance.*cos(b));
xx = abs(sum(Sto,2));
yy = abs(sum(Ste,2));
Note that you have b=u above, so that Sto == Ste, xx == yy, correlation is 1, and mse is 0. I suspect that your mse calculation is not what you really want (see comments in the code below, and maybe test with a case where b ~= u, so that mse is not zero, e.g., b = u+1, to see the difference between the two mse calculations).
%%%%%%%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%%%%%%%
correlation = corr(xx,yy)
correlation = 1
% mse1 = zeros(1,NE);
% for pp=1:NE
% mse1=(xx(pp)-yy(pp)); % I assume you meant "mse1(pp)" here instead of "mse1"
% end
mse = sum(xx-yy,1).^2/NE % this is equivalent to what you had (with the mse1(pp) correction in the loop above) ...
mse = 0
mse = mean((xx-yy).^2,1) % ... but this is MSE
mse = 0
mse_corr = mse + correlation-1 % note that correlation is a real scalar, so norm(correlation) == correlation (i.e., norm() does nothing)
mse_corr = 0

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 3일
Here is the vectorized code without any loops:
th=pi/180;
u=[40*th 50*th 60*th 70*th];
b=u;
K=length(u);
% Note that N<M coprime No
N=3; M=4;
NE=(N+M-1); % Total
distance = [0*pi*N; 1*pi*N; 1*pi*M; 2*pi*N; 2*pi*M; 3*pi*N];
STO_1 = exp(-1i*distance(:)*cos(u));
y_act_sum=sum(Sto,2);
y_act_abs=abs(y_act_sum);
xx=(y_act_abs);
STE_1 = exp(-1i*distance(:)*cos(b));
%%%%%%%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%%%%%%%%
y_est_sum=sum(Ste,2);
y_est_abs=abs(y_est_sum);
yy=(y_est_abs);
correlation=corr(xx,yy);
mse1=(xx-yy);
mse2=sum(mse1,2);
mse3=mse2.^2;
mse=mse3/NE;
mse_corr=mse+norm(correlation-1);

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by