# Problem with code-beginner

조회 수: 1(최근 30일)
Bianca Brisc 2021년 6월 15일
편집: David Hill 2021년 6월 15일
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;
##### 댓글 수: 1표시 없음숨기기 없음
dpb 2021년 6월 15일
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 2021년 6월 15일
편집: David Hill 2021년 6월 15일
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

댓글을 달려면 로그인하십시오.

### 범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by