How to vectorize the given code?

조회 수: 3 (최근 30일)
Sadiq Akbar
Sadiq Akbar 2024년 1월 7일
댓글: Sadiq Akbar 2024년 1월 7일
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
Sadiq Akbar
Sadiq Akbar 2024년 1월 7일
Thanks a lot dear Torsten for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
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)
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