How to vectorize this piece of code and why doesn't e come out to be zero though it must come out to be zero because u and b are equal?
古いコメントを表示
u=[30 50 75 -30 -50 -75];
b=u;
Noise=5;
M = 6;%Constant1
N = 6;%Constant2
d = 0.5;%Constant3
K = length(u)/2; %Constant4
alpha=ones(1,K);%[1 1 1 1 1];
a=zeros(M,K);%aTx
f=zeros(N,K);%fRx
c=zeros(M*N, length(u)-K);% Extended Matrix
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%
% Observed response
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
a(h,i)=exp(j*2*pi*(h-1)*d*sind(u(i))); %%%%% a
end
for p=1:N
f(p,i)=exp(j*2*pi*(p-1)*d*sind(u(K+i))); %%%%% f
end
end
for g= 1:K
c(:,g)=kron(a(:,g),f(:,g));% Extended vector
end
yo=c*alpha'; % Observed Response (Noise Free)
yo=awgn(yo,Noise);% Uncomment for Noise consideration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimated response
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ae=zeros(M,K);
fe=zeros(N,K);
ce=zeros(M*N, length(u)-K);
for i=1:K
for h=1:M
ae(h,i)=exp(j*2*pi*(h-1)*d*sind(b(i))); %%%%% ae
end
for p=1:N
fe(p,i)=exp(j*2*pi*(p-1)*d*sind(b(K+i))); %%%%% fe
end
end
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
ye=ce*alpha';% Estimated Response
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e=0.0;
for s=1:M*N
e=e+(abs(yo(s,1)-ye(s,1))).^2;
end
e=e/M*N;
採用された回答
その他の回答 (1 件)
Walter Roberson
2022 年 12 月 10 日
編集済み: Walter Roberson
2022 年 12 月 10 日
format long g
u=[30 50 75 -30 -50 -75];
b=u;
Noise=5;
M = 6;%Constant1
N = 6;%Constant2
d = 0.5;%Constant3
K = length(u)/2; %Constant4
alpha=ones(1,K);%[1 1 1 1 1];
a=zeros(M,K);%aTx
f=zeros(N,K);%fRx
c=zeros(M*N, length(u)-K);% Extended Matrix
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%
% Observed response
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
a(h,i)=exp(j*2*pi*(h-1)*d*sind(u(i))); %%%%% a
end
for p=1:N
f(p,i)=exp(j*2*pi*(p-1)*d*sind(u(K+i))); %%%%% f
end
end
for g= 1:K
c(:,g)=kron(a(:,g),f(:,g));% Extended vector
end
yo=c*alpha'; % Observed Response (Noise Free)
yo=awgn(yo,Noise);% Uncomment for Noise consideration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimated response
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ae=zeros(M,K);
fe=zeros(N,K);
ce=zeros(M*N, length(u)-K);
for i=1:K
for h=1:M
ae(h,i)=exp(j*2*pi*(h-1)*d*sind(b(i))); %%%%% ae
end
for p=1:N
fe(p,i)=exp(j*2*pi*(p-1)*d*sind(b(K+i))); %%%%% fe
end
end
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
ye=ce*alpha';% Estimated Response
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e=0.0;
for s=1:M*N
e=e+(abs(yo(s,1)-ye(s,1))).^2;
end
e=e/(M*N);
e2 = sum( abs(yo - ye).^2 )
yo - ye
3 件のコメント
Sadiq Akbar
2022 年 12 月 10 日
編集済み: Sadiq Akbar
2022 年 12 月 10 日
Walter Roberson
2022 年 12 月 10 日
I thought I had canceled that post...
I do not see any reason why the values should be the same. awgn() applies randomness, but your ye has not had any randomness applied to it (certainly not exactly the same randomness). There is no reason for them to be the same.
Sadiq Akbar
2022 年 12 月 10 日
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!