How to vectorize the given code?

3 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2024-1-7
移动:Torsten 2024-1-7
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

更多回答(1 个)

Sulaymon Eshkabilov
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 个评论
Torsten
Torsten 2024-1-7
编辑:Torsten 2024-1-7
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 2024-1-7
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Model Building and Assessment 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by