How to vectorize the given code?

1 view (last 30 days)
Sadiq Akbar
Sadiq Akbar on 7 Jan 2024
Commented: Sadiq Akbar on 7 Jan 2024
u=[1 2 40 70];b=u;
[~,C]=size(b);
P=C/2;
M=2*C;
f=1e9;
c=3e8;
l=c/f;
K=(2*pi)/l;
M=10;
d_circular=l/2;
a=(M*d_circular)/(2*pi);
xo=zeros(1,M);
xe=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+exp(-1i*K*a*sind(u(i))*cosd(u(P+i)-(2*pi*(k-1)/M)));
xe(1,k)=xe(1,k)+exp(-1i*K*a*sind(b(i))*cosd(b(P+i)-(2*pi*(k-1)/M)));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (MSE)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e = mean(abs(xo-xe).^2);
e = 0

Accepted Answer

Torsten
Torsten on 7 Jan 2024
Moved: Torsten on 7 Jan 2024
u = [1, 2, 30, 50];b=u;
dim = length(u);
N=dim;
s_u = u(1:dim/2)';
angles_u = u((dim/2)+1:end)';
s_b = b(1:dim/2)';
angles_b = b(1+dim/2:end)';
lambda = 1;
d = lambda/2;
% Steering matrix
Ao = zeros(N, length(angles_u));
Ae = zeros(N, length(angles_b));
for i = 1:N
for j = 1:length(angles_u)
Ao(i, j) = exp(1j * 2 * pi * d * (i-1) / lambda * sind(angles_u(j)));
Ae(i, j) = exp(1j * 2 * pi * d * (i-1) / lambda * sind(angles_b(j)));
end
end
xo=Ao*s_u
xo =
3.0000 + 0.0000i -1.4837 + 2.3412i -0.7987 - 1.9898i 1.1850 + 0.6111i
xe=Ae*s_b
xe =
3.0000 + 0.0000i -1.4837 + 2.3412i -0.7987 - 1.9898i 1.1850 + 0.6111i
% MSE
e=mean(abs(xo-xe).^2)
e = 0
i = 1:N;
j = 1:length(angles_u);
Ao = (exp(1j * 2 * pi * d * (i-1) / lambda .* sind(angles_u(j)))).';
Ae = (exp(1j * 2 * pi * d * (i-1) / lambda .* sind(angles_b(j)))).';
xo=Ao*s_u
xo =
3.0000 + 0.0000i -1.4837 + 2.3412i -0.7987 - 1.9898i 1.1850 + 0.6111i
xe=Ae*s_b
xe =
3.0000 + 0.0000i -1.4837 + 2.3412i -0.7987 - 1.9898i 1.1850 + 0.6111i
% MSE
e=mean(abs(xo-xe).^2)
e = 0

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 7 Jan 2024
One quick comment is that xe and xo have the same values and therefore, the error is "0".
u=[1 2 40 70];b=u;
[~,C]=size(b);
P=C/2;
M=2*C;
f=1e9;
c=3e8;
l=c/f;
K=(2*pi)/l;
M=10;
d_circular=l/2;
a=(M*d_circular)/(2*pi);
xo=zeros(1,M);
xe=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+exp(-1i*K*a*sind(u(i))*cosd(u(P+i)-(2*pi*(k-1)/M)));
xe(1,k)=xe(1,k)+exp(-1i*K*a*sind(b(i))*cosd(b(P+i)-(2*pi*(k-1)/M)));
end
end
plot(1:numel(xo),real(xo), 'bo-','LineWidth', 2.5, 'DisplayName', 'Re(x_o)')
hold on
plot(1:numel(xe),real(xe), 'r-','LineWidth', 1.5, 'DisplayName', 'Re(x_e)')
legend('show')
figure
plot(1:numel(xo),imag(xo), 'bo-','LineWidth', 2.5, 'DisplayName', 'Im(x_o)')
hold on
plot(1:numel(xe),imag(xe), 'r-','LineWidth', 1.5, 'DisplayName', 'Im(x_e)')
legend('show')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (MSE)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e = mean(abs(xo-xe).^2)
e = 0
  5 Comments
Torsten
Torsten on 7 Jan 2024
Edited: Torsten on 7 Jan 2024
Please include the code you want to vectorize (the one with the working for-loops).
I doubt the result for Ao and Ae will be of dimension N x length(angles_u).
Sadiq Akbar
Sadiq Akbar on 7 Jan 2024
Thanks a lot dear @Torsten for your prompt response. The whole code is as below:
u = [1, 2, 30, 50];b=u;
dim = length(u);
N=dim;
s_u = u(1:dim/2)';
angles_u = u((dim/2)+1:end)';
s_b = b(1:dim/2)';
angles_b = b(1+dim/2:end)';
lambda = 1;
d = lambda/2;
% Steering matrix
Ao = zeros(N, length(angles_u));
Ae = zeros(N, length(angles_b));
for i = 1:N
for j = 1:length(angles_u)
Ao(i, j) = exp(1j * 2 * pi * d * (i-1) / lambda * sind(angles_u(j)));
Ae(i, j) = exp(1j * 2 * pi * d * (i-1) / lambda * sind(angles_b(j)));
end
end
xo=Ao*s_u;
xe=Ae*s_b;
% MSE
e=mean(abs(xo-xe).^2)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!