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);
댓글 수: 0
채택된 답변
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
xe=Ae*s_b
% MSE
e=mean(abs(xo-xe).^2)
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
xe=Ae*s_b
% MSE
e=mean(abs(xo-xe).^2)
추가 답변 (1개)
Sulaymon Eshkabilov
2024년 1월 7일
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)
댓글 수: 5
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!