Matlab for loops output

1 view (last 30 days)
Anum Ahmed
Anum Ahmed on 6 Jul 2018
Commented: Anum Ahmed on 6 Jul 2018
Dear all, I am trying to solve the following loops in MATLAB.
N=4;
d=0.0019;
fc=77e9;
c=3e8;
tdr1=5.33e-7;
Sig = zeros(10:4);
for i=1:N
for AzAng_1 = -90:20:90
Delay_Apos = d*sind(AzAng_1)*((N+1)/2-i)/(c);
Delay_Apos = tdr1 + Delay_Apos;
Steer_Vec = exp(-1i*2*pi*fc*Delay_Apos);
Sig(:,i) = Steer_Vec;
end
i=i+1;
end
Sig
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
At the output, I want to have a matrix of order [10:4] with each row containing the output of 'inner for loop' having a specific value of i. I.e, First column (i=1) should have 10 different values of the 'inner for loop'. After these 10 inner iterations, i value should be updated and now with i=2, inner for loop should be solved and now, the 10 values of 'inner for loop' should be arranged in the 2 column of 'Sig'.
I am trying to solve it, but I only get the last output i.e a (1:4) order signal instead of a (10:4) Signal I need.
I have been working on it, but am unable to get the output I want. Help would be appreciated
Regards

Accepted Answer

dpb
dpb on 6 Jul 2018
Edited: dpb on 6 Jul 2018
In Sig(:,i) = Steer_Vec; you have the indices reversed relative to the way you sized the array ---
ERRATUM: Bad eyes... :(
Actually, you didn't compute the Delay_Apos as a vector...
...
AzAng_1 = -90:20:90;
for i=1:N
Delay_Apos = d*sind(AzAng_1)*((N+1)/2-i)/(c);
Delay_Apos = tdr1 + Delay_Apos;
Steer_Vec = exp(-1i*2*pi*fc*Delay_Apos);
Sig(:,i) = Steer_Vec;
end
with a loop over i
With meshgrid and recasting the formulae for vector operation you could remove the loop entirely.
  4 Comments
dpb
dpb on 6 Jul 2018
No problem, sorry I got my eyes crossed first go...as noted it can be even more succinct using the ML vector notation. As per usual, one trades some memory for holding temporaries for more efficient code.
K=1i*2*pi*fc; % define a group constant for convenience
% write functions as anonymous -- kept same factoring here..
% NB: dot operators to operate on element basis over arrays
fnDelay = @(ang,idx) tdr1 + d*sind(ang).*((N+1)/2-idx)/c;
fnVec = @(dly) exp(-K*dly);
AzAng = -90:20:90;
[a,ix]=meshgrid(AzAng,1:N); % generate the grid points
Sig=fnVec(fnDelay(a,ix)).'; % and evaluate...
Anum Ahmed
Anum Ahmed on 6 Jul 2018
Thankyou so much for your kind suggestion. Much appreciated.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!