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 Center および File ExchangeModel Building and Assessment についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by