the code of "myfunAskMathworks.m", yo and ye both are of the order 36x1 and these are correct. But in the other 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. myfunAskMathworks.m u=[10 20 30 40];b=u; M=6; % Tx constant N=6; % Rx constant 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); % 36x1 in which 36 is due to M*N ye = yMatTR(deg2rad(b), steerVecT, steerVecR); % 36x1 in which 36 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 codeWithArrays.m close all; clear all; clc; u=[10 20 30 40];b=u; az = u(1:2)'; %Azimuths el = u(3:4)'; % Elevations M = length(az); % No. of sources L = 1; m = randn(M,L); % Wavenumber vectors k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% Always 3 x No. of sources % Tx antennas N = 6; % Array geometry [rx,ry,rz] (example: uniform circular array) radius = 0.5/sind(180/N); rx = radius*cosd(360*(0:N-1).'/N); ry = radius*sind(360*(0:N-1).'/N); rT = [rx, ry, zeros(N,1)];% Always No. of Antennas x 3 % Rx antennas M=6; % Array geometry [rx,ry,rz] (example: uniform circular array) radius = 0.5/sind(180/M); rx = radius*cosd(360*(0:M-1).'/M); ry = radius*sind(360*(0:M-1).'/M); rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3 % Matrices AT and AR AT = exp(-1j*rT*k);% Always (TX Antennas x No. of sources) AR = exp(-1j*rR*k);% Always (RX Antennas x No. of sources) az=az'; el=el'; yo = yMatTR(deg2rad(az), AT, AR); % 36x1 in which 36 is due to M*N ye = yMatTR(deg2rad(el), AT, AR); %%%%%%%%%%%%%%%%%% % 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
Prashant Kumar answered .
2025-11-20
The key difference between the two scripts likely lies in how data types, dimensions, or operations are handled in codeWithArrays.m compared to myfunAskMathworks.m. Specifically, the error on line 35 of codeWithArrays.m may arise due to differences in:
Input Data Dimensions: If the input data (u or b) in codeWithArrays.m has a different size or structure compared to myfunAskMathworks.m, this can lead to dimension mismatches inside the yMatTR function.
Function Usage and Broadcast Behavior: In yMatTR, the usage of operations like permute and element-wise multiplication (.*) assumes specific dimensions for the steerA and steerB arrays. A mismatch in dimensions could result in a runtime error.
Anonymous Function Handling: Ensure that steerVecT and steerVecR are used in the same manner in both scripts. Any difference in the way they are defined or called may cause unexpected behavior.
Complex Data and Real-Imaginary Splits: MATLAB handles complex data carefully, but if there are any implicit real-imaginary splits or type conversions in codeWithArrays.m, they might cause inconsistencies.
Check Input Dimensions: Ensure that the dimensions of u and b in codeWithArrays.m match those in myfunAskMathworks.m. For example, they should be row vectors of size 1x4 as expected by vecH.
Validate steerVecT and steerVecR: Verify that these functions generate arrays of the correct size in both scripts. They are expected to produce arrays of size (M x length(u)) and (N x length(u)) respectively.
Inspect yMatTR Behavior: The line steerA.*permute(steerB,[3,2,1]) assumes that steerA and steerB align dimensionally for broadcasting. Insert debugging statements such as disp(size(steerA)) and disp(size(steerB)) before this operation in codeWithArrays.m to ensure they match.
Compare Outputs: Print intermediate outputs in both scripts to see where they start to diverge. For example, compare the output of steerVecT, steerVecR, and yMatTR.
Use Explicit Reshaping if Needed: If dimension mismatches are found, explicitly reshape inputs to ensure they align. For instance:
steerB = permute(steerB, [3,2,1]); % Adjust dimensions explicitly