How to remove all for-loops with vectorization?

I have the following piece of code. I have posted similar codes earlier also and therefore 1st I tried myself to vectorize it but coudn't succeed. How can we replace all for-loops by making it vectorized code?
u=[35 75 -35 -75 0.3 0.5];
b=u;
K=length(u)/3;% Constant1
u1=u(1:2*K);
alpha=u(2*K+1:end);
M = 10; % Consatn2
N = 6; % Consatn3
s1=zeros(M,K);
s2=zeros(N,K);
C=zeros(M*N, length(u1)-K);
%%%%%%%%%%%%%%%%%%%
% Xo Calculation
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
s1(h,i)=exp(j*2*pi*(h-1)*0.5*sind(u1(i)));
end
for p=1:N
s2(p,i)=exp(j*2*pi*(p-1)*0.5*sind(u1(K+i)));
end
end
for g= 1:K
C(:,g)=kron(s1(:,g),s2(:,g));
end
Xo=C*alpha';
%%%%%%%%%%%%%%%%%%%
% Xe Calculation
%%%%%%%%%%%%%%%%%%%
bu=b(1:2*K); alphab=b(2*K+1:end);
s1_e=zeros(M,K);
s2_e=zeros(N,K);
C_e=zeros(M*N, length(u1)-K);
for i=1:K
for h=1:M
s1_e(h,i)=exp(j*2*pi*(h-1)*0.5*sind(bu(i)));
end
for p=1:N
s2_e(p,i)=exp(j*2*pi*(p-1)*0.5*sind(bu(K+i)));
end
end
for g= 1:K
C_e(:,g)=kron(s1_e(:,g),s2_e(:,g));
end
Xe=C_e*alphab';
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=0.0;
for s=1:M*N
e=e+(abs(Xo(s,1)-Xe(s,1))).^2;
end
e=e/M*N

 Respuesta aceptada

This code should give you a pretty good idea how to do this:
clear
clc
u=[35 75 -35 -75 0.3 0.5];
b=u;
K=length(u)/3;% Constant1
u1=u(1:2*K);
%alpha=u(2*K+1:end);
M = 10; % Consatn2
N = 6; % Consatn3
s1=zeros(M,K);
s2=zeros(N,K);
C=zeros(M*N, length(u1)-K);
%%%%%%%%%%%%%%%%%%%
% Xo Calculation
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
s1(h,i)=exp(j*2*pi*(h-1)*0.5*sind(u1(i)));
end
for p=1:N
s2(p,i)=exp(j*2*pi*(p-1)*0.5*sind(u1(K+i)));
end
end
% Vectorization approach
i = 1:K;
s1_n=zeros(M,K);
s2_n=zeros(N,K);
h = 1:M;
s1_n(h,i)=exp(j*2*pi*(h-1).'*0.5*sind(u1(i)));
p = 1:N;
s2_n(p,i)=exp(j*2*pi*(p-1).'*0.5*sind(u1(K+i)));
% check if there is a difference
norm(s1-s1_n)
ans = 0
norm(s2-s2_n)
ans = 0

11 comentarios

Sadiq Akbar
Sadiq Akbar el 18 de En. de 2023
Editada: Sadiq Akbar el 18 de En. de 2023
Thank you very much dear Askic V for your kind response. I have some obervations:
1-1st you have not considered the last two values of u=[35 75 -35 -75 0.3 0.5]; but these values are must. Also you have commented alpha and have not included it in calculation but it is must.
2-You have not calculated C(:,g)=kron(s1(:,g),s2(:,g)); which is also must.
3-Calculation of both Xo and Xe are must, but you have not calculated them.
4-I don't want just error but I want mean sqaure error which you have not calculated.
Askic V
Askic V el 18 de En. de 2023
Helo Sadiq. The code snippet was just to show you how it is done. I have commented alpha because I did not use it in the code.
I'm pretty sure you won't have any problems completing the rest by yourself.
Sadiq Akbar
Sadiq Akbar el 19 de En. de 2023
Thank you very much dear Askic V for your kind response. Yes, I do agree that you have not used the last two values of u and alpha but as I requested, these are must in calculation. Please, do include these two values also, after that I will try to convert the rest of the code myself in sha Allah.
Sadiq Akbar
Sadiq Akbar el 19 de En. de 2023
Sorry one thing more. Please also calculate MSE instead of just difference. Regards,
I included the last two values and alpha and tried like this:
clear
clc
u=[35 75 -35 -75 0.3 0.5];
b=u;
K=length(u)/3;% Constant1
u1=u(1:2*K); b1=b(1:2*K);
alpha=u(2*K+1:end);alphab=b(2*K+1:end);
M = 10; % Consatn2
N = 6; % Consatn3
s1=zeros(M,K);
s2=zeros(N,K);
C=zeros(M*N, length(u1)-K);
%%%%%%%%%%%%%%%%%%%
% Xo Calculation
%%%%%%%%%%%%%%%%%%%
i = 1:K;
s1=zeros(M,K);
s2=zeros(N,K);
C=zeros(M*N, length(u1)-K);% By Me
h = 1:M;
s1(h,i)=exp(j*2*pi*(h-1).'*0.5*sind(u1(i)));
p = 1:N;
s2(p,i)=exp(j*2*pi*(p-1).'*0.5*sind(u1(K+i)));
for g= 1:K
C(:,g)=kron(s1(:,g),s2(:,g));
end
Xo=C*alpha';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Xe Calculation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i = 1:K;
s1_n=zeros(M,K);
s2_n=zeros(N,K);
h = 1:M;
s1_n(h,i)=exp(j*2*pi*(h-1).'*0.5*sind(u1(i)));
p = 1:N;
s2_n(p,i)=exp(j*2*pi*(p-1).'*0.5*sind(u1(K+i)));
%%%%%%%%%%%%%%%5555
% Below is mine
%%%%%%%%%%%%%%%%%%%
for g= 1:K
C_e(:,g)=kron(s1_n(:,g),s2_n(:,g));
end
Xe=C_e*alphab';
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(Xo-Xe).^2/(M*N);
But I cannot replace the sinlge for-loop in each of Xo and Xe sections. Further, I don't know whether it is corret or not?
Hello Sadiq,
I have used _n (n as new) to get results using vectorization approach, so I can compare with original arrays and determine if there is a difference.
Regarding the last for loop, do you want to eliminate for loop or you need to make this as efficient as it can be?
I can provide the solution without for loop but it wouldn't be much efficient (maybe even worse than for loop). I hope someone more knowledgeable can jump in and help.
clear
clc
a = rand(3, 2);
b = rand(4, 2);
for k = 1:2
C(:,k) = kron(a(:,k), b(:,k));
end
k = 1:2;
D = kron(a(:,k),b(:,k));
D(:,2:end-1)=[];
C-D
ans = 12×2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sadiq Akbar
Sadiq Akbar el 19 de En. de 2023
Thank you very much dear Askic V for your kind response. This code works only for 3x2 and 4x2 matrices a and b. If we want to increase their order to 3x4 and 4x4, then it fails to work.
Then a little adjustment is needed:
clear
clc
N = 4;
a = rand(3, N);
b = rand(4, N);
for k = 1:N
C(:,k) = kron(a(:,k), b(:,k));
end
k = 1:N;
D = kron(a(:,k),b(:,k));
col_ind = mod(0:size(D,2)-1, 1+N) < 1;
D(:, ~col_ind) = [];
C-D
ans = 12×4
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Thank you very much dear Askic V for your kind response. Ok I am going to accept your answer but fore that let me confirm from you 1st. Have I vectorized the code correctly like you which I have shared above i.e.:
clear
clc
u=[35 75 -35 -75 0.3 0.5];
b=u;
K=length(u)/3;% Constant1
u1=u(1:2*K); b1=b(1:2*K);
alpha=u(2*K+1:end);alphab=b(2*K+1:end);
M = 10; % Consatn2
N = 6; % Consatn3
s1=zeros(M,K);
s2=zeros(N,K);
C=zeros(M*N, length(u1)-K);
%%%%%%%%%%%%%%%%%%%
% Xo Calculation
%%%%%%%%%%%%%%%%%%%
i = 1:K;
s1=zeros(M,K);
s2=zeros(N,K);
C=zeros(M*N, length(u1)-K);% By Me
h = 1:M;
s1(h,i)=exp(j*2*pi*(h-1).'*0.5*sind(u1(i)));
p = 1:N;
s2(p,i)=exp(j*2*pi*(p-1).'*0.5*sind(u1(K+i)));
for g= 1:K
C(:,g)=kron(s1(:,g),s2(:,g));
end
Xo=C*alpha';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Xe Calculation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i = 1:K;
s1_n=zeros(M,K);
s2_n=zeros(N,K);
h = 1:M;
s1_n(h,i)=exp(j*2*pi*(h-1).'*0.5*sind(u1(i)));
p = 1:N;
s2_n(p,i)=exp(j*2*pi*(p-1).'*0.5*sind(u1(K+i)));
for g= 1:K
C_e(:,g)=kron(s1_n(:,g),s2_n(:,g));
end
Xe=C_e*alphab';
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(Xo-Xe).^2/(M*N);
Well, two for loops are correctly replaced with vectorization. However it seems that you have a lot of unnecessary (redundant) code.
You repeat calculations for s1 and s2 just to calculate Xe, signals b and u are the same, alpha and alphab have the same values. Therefore Xe and Xo have the same values.
Once gain, this is correct, change the code with for loops:
for i=1:K
for h=1:M
s1_e(h,i)=exp(j*2*pi*(h-1)*0.5*sind(bu(i)));
end
for p=1:N
s2_e(p,i)=exp(j*2*pi*(p-1)*0.5*sind(bu(K+i)));
end
end
to vectorized version:
i = 1:K;
h = 1:M;
s1(h,i)=exp(j*2*pi*(h-1).'*0.5*sind(u1(i)));
p = 1:N;
s2(p,i)=exp(j*2*pi*(p-1).'*0.5*sind(u1(K+i)));
Sadiq Akbar
Sadiq Akbar el 21 de En. de 2023
Thank you very much dear Askic V for your kind response.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 18 de En. de 2023

Comentada:

el 21 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by