# Why the 2nd code does not behave like the 1st code?

2 views (last 30 days)
Sadiq Akbar on 27 Feb 2023
Commented: Sadiq Akbar on 3 Mar 2023
In the attached code of "myfunAskMathworks.m", yo and ye both are of the order 36x1 and these are correct. But in the other attached code "codeWithArrays.m", it gives error on line 35. Why is it so? In this also we should get the same order of yo and ye but it gives error.

Askic V on 3 Mar 2023
Edited: Askic V on 3 Mar 2023
This is how I would modified the code to execute for any M r N dimensions:
clear
clc
u = [10 20 30 40];
b = u * (1+0.5*randn); % random deviation between u and b
% Tx antennas
N = 6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
rT = [rx, ry, zeros(N,1)];% Always No. of Antennas x 3
% Rx antennas
M = 10;
% Array geometry [rx,ry,rz] (example: uniform circular array)
rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3
% rT is size Nx3, so to make multiplication work,
% [cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)] must be 3x1
AT = @(alpha, beta) exp(-1j.*rT*pi*[cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)]);
% rR is size Mx3, so to make multiplication work,
% [cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)] must be 3x1
AR = @(alpha, beta) exp(-1j.*rR*pi*[cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of yo and ye
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yo = yMatTR(u, AT, AR);
ye = yMatTR(b, AT, AR);
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function yMatTR
%%%%%%%%%%%%%%%%%%%%%%%%5%%%
function y = yMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(az,el);
steerB = steerVecR(az,el);
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
Sadiq Akbar on 3 Mar 2023
Thanks a lot dear Askic V for your kind response. Yes, now it works. Thank you once again.

Askic V on 27 Feb 2023
Edited: Askic V on 27 Feb 2023
It is because in the function "myfunAskMathworks.m" you have function handles defined. So, you are sending the function handle as an input parameter to the function yMatTR.
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
In the line:
steerA = steerVecT(targetAngle)
you are actually calling the function handle for angles as an input to the function.
In the other file:
AT = exp(-1j*rT*k);
this is not a function, but an array (matrix) of complex numbers.
So in the line:
steerA = steerVecT(targetAngle);
you are trying to access elements of a matrix with indices that are not either integer or logical values.
Essentially, you are confusing function handles with matrices.
Sadiq Akbar on 3 Mar 2023
I tried to make the two codes appear the same to you and you understand it better. So I made some changes for you and also added comments for your understanding now. Look at the 1st code below:
clear; clc;
u=[35 39 127 63 14 57];b=u; % Signal source directions
M=10;% Tx antennas
N=10;% Rx antennas
d = 0.5; % constant
vecH = @(MAT) MAT(:).';
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
steerVecR = @(ang) exp(1j*2*pi*d*(0:N-1).'*sin(vecH(ang)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of yo and ye
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yo = yMatTR(deg2rad(u), steerVecT, steerVecR); % 100x1 in which 100 is due to M*N
ye = yMatTR(deg2rad(b), steerVecT, steerVecR); % 100x1 in which 100 is due to M*N
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function yMatTR
%%%%%%%%%%%%%%%%%%%%%%%%5%%%
function y = yMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(targetAngle);
steerB = steerVecR(targetAngle);
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
It is ok and works well. I want to make changes in the following 2nd code so that it also behaves like the above code. The 2nd code is below:
clear; clc;
% Signal source directions
az = [35;39;127]; % Azimuths
el = [63;14;57]; % Elevations
K = length(az); % Number of sources
Am=ones(K,1);% Amplitudes
% ========= (2) RECEIVED SIGNAL ========= %
% Wavenumber vectors (in units of wavelength/2)
k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% 3 x No. of sources
% Constructing Antenna array
N = 10; % Number of antennas
% Array geometry [rx,ry,rz] (example: uniform circular array) in 3D space
r = [rx, ry, zeros(N,1)];% 10 x 3 i.e., No. of Antennas x 3Dimensional Space
% Note: r denotes the final antenna array in the form of circle in 3D space
% Matrix of array response vectors
A = exp(-1j*r*k);% 10 x 2 i.e., No. of Antennas x No. of sources
x = A*Am; % 10 x 1 i.e., No. of antennas x 1
I hope, now it will be easy for you to understand.