# Problem with code-beginner

1 view (last 30 days)
Bianca Brisc on 15 Jun 2021
Edited: David Hill on 15 Jun 2021
I have this code, and I receive the following error:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in antennas3sector_final (line 21)
distances(site,r,c) = sqrt((x(r,c,site)+xshift(site)).^2+(y(r,c,site)+yshift(site)).^2);
distances = zeros(length(xx),length(yy),NumberOfSites);
for site = 1:NumberOfSites
for r = 1:length(distances)
for c = 1:length(distances)
xshift(site) = isd*sind(AngleDistance*(site-1));
yshift(site) = isd*cosd(AngleDistance*(site-1));
distances(site,r,c) = sqrt((x(r,c,site)+xshift(site)).^2+(y(r,c,site)+yshift(site)).^2);
end;
end;
end;
dpb on 15 Jun 2021
for site = 1:NumberOfSites
for r = 1:length(distances)
for c = 1:length(distances)
xshift(site) = isd*sind(AngleDistance*(site-1));
...
What is (site-1) when site==1 the first pass thru the loop???

David Hill on 15 Jun 2021
Edited: David Hill on 15 Jun 2021
distances = zeros(length(xx),length(yy),NumberOfSites);
xshift = isd*sind(AngleDistance*((1:NumberOfSites)-1));%assume isd and AngleDistance is a constant
yshift = isd*cosd(AngleDistance*((1:NumberOfSites)-1));%calculate outside loop
for site = 1:NumberOfSites
distances(:,:,site) = sqrt((x(:,:,site)+xshift(site)).^2+(y(:,:,site)+yshift(site)).^2);
end

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by