# How to reduce its execution time and why the value of e is a column vector?

2 ビュー (過去 30 日間)
Sadiq Akbar 2023 年 1 月 2 日
コメント済み: Sadiq Akbar 2023 年 1 月 9 日
I have the following piece of code.
clc;clear all;
c = 340;
f = 3400;
d = c/f/2;
T = 1;
Sig = 2;
M = 6;
N = 7;
theta = linspace(-60, 60, Sig);
p = 6;
SNR = 10;
Fc=[2*10^3:2*10^3/(Sig-1):5*10^3];
T_Vector=1/f;
p_N = [0:M/p:M*(N-1)/p];
p_M = [0:N:(M-1)*N];
P = union(p_N,p_M);
A = zeros(length(P),Sig);
SigVec = zeros(Sig,T);
for Q = 1:Sig
A(:,Q) = exp(-j*P'*2*pi*d*sin(theta(Q)*pi/180)*f/c);
SigVec(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
end
%%%%%%%%%%%%%%%%%%
% xo calculation
%%%%%%%%%%%%%%%%%%
xo = A*SigVec;
%%%%%%%%%%%%%%%%%%%%%%%%%%
% xe calculation
%%%%%%%%%%%%%%%%%%%%%%%%%
b=theta;
for Q = 1:Sig
Ae(:,Q) = exp(-j*P'*2*pi*d*sin(b(Q)*pi/180)*f/c);
SigVec_est(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
end
xe = Ae*SigVec_est;
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%
e = mean(abs(xo-xe).^2,2)
I want to reduce its execution time. Further, I want a single value of e as zero but it gives me a column vector of e.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sadiq Akbar 2023 年 1 月 3 日
Can you replace "Just the for-loops here by using concept of vectorization?". Leave the rest of the code, just replace the two for-loops here.

サインインしてコメントする。

### 採用された回答

Walter Roberson 2023 年 1 月 2 日
you are taking the mean over the second dimension of a column vector (which has a second dimension of length 1)
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sadiq Akbar 2023 年 1 月 2 日
Thanks a lot for your guidance dear Walter Roberson. Yes you are right. When I removed the 2 after comma, it gives single value of e now.

サインインしてコメントする。

### その他の回答 (1 件)

Voss 2023 年 1 月 3 日
Note that SigVec has T columns because of its pre-allocation, but that SigVec_est has one column because it was not pre-allocated in the original code. I've kept them like that (change T to something > 1 to see the difference).
clc;clear all;
c = 340;
f = 3400;
d = c/f/2;
T = 1;
Sig = 2;
M = 6;
N = 7;
theta = linspace(-60, 60, Sig);
p = 6;
SNR = 10;
Fc=[2*10^3:2*10^3/(Sig-1):5*10^3];
T_Vector=1/f;
p_N = [0:M/p:M*(N-1)/p];
p_M = [0:N:(M-1)*N];
P = union(p_N,p_M);
% A = zeros(length(P),Sig);
% SigVec = zeros(Sig,T);
%
% for Q = 1:Sig
% A(:,Q) = exp(-j*P'*2*pi*d*sin(theta(Q)*pi/180)*f/c);
% SigVec(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
% end
A = exp(-1j*P(:)*2*pi*d.*sin(theta*pi/180)*f/c);
SigVec = exp(1j*2*pi*Fc(:).*T_Vector*ones(1,T));
%%%%%%%%%%%%%%%%%%
% xo calculation
%%%%%%%%%%%%%%%%%%
xo = A*SigVec;
%%%%%%%%%%%%%%%%%%%%%%%%%%
% xe calculation
%%%%%%%%%%%%%%%%%%%%%%%%%
b=theta;
% for Q = 1:Sig
% Ae(:,Q) = exp(-j*P'*2*pi*d*sin(b(Q)*pi/180)*f/c);
% SigVec_est(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
% end
Ae = exp(-1j*P(:)*2*pi*d.*sin(b*pi/180)*f/c);
SigVec_est = exp(1j*2*pi*Fc(:).*T_Vector);
xe = Ae*SigVec_est;
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%
e = mean(abs(xo-xe).^2,2)
e = 12×1
0 0 0 0 0 0 0 0 0 0
##### 14 件のコメント12 件の古いコメントを表示12 件の古いコメントを非表示
Sadiq Akbar 2023 年 1 月 9 日
Ok thank you very much dear Walter Roberson for your kind responses and help.
Sadiq Akbar 2023 年 1 月 9 日
Thank you also dear Voss for your responses and help.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeClassical Control Design についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by