Index in position 2 exceeds array bounds (must not exceed 1).
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello, I'm trying to run this code (gauss elimination), but i get an error message :Index in position 1 exceeds array bounds (must not exceed 1). the error message comes from ''while tmp<=n && IS(tmp,1)~=0 '' . What does "Index in position 1 exceeds array bounds (must not exceed 1)" mean? and how to solve this?
Here is code :
function [RL,HI]=inputt(BM,BS,IS,FS)
if nargin<4, error("4 Inputs required"),end
n=length(BS);
RL(1,1)=BM;
for i=1:n
HI(i,1) = BS(i,1)+BS(i,1);
tmp = i+1;
while tmp<=n && IS(tmp,1)~=0
RL(tmp,1) = HI(i,1)-IS(tmp,1);
tmp=tmp+1;
end
if tmp<=n
RL(tmp,1) = HI(i,1)-FS(tmp,1);
end
i = tmp;
end
댓글 수: 0
답변 (1개)
Karim
2022년 8월 25일
The error is due to the way you are accesing the data, based on you picture the inputs are row vectors. IS is a 1x6 vector. However in the script, you are trying to acces the data as a column vector --> IS (tmp,1) which fails since the input is a row vector.
There are two solutions, either change the input into column vectors (as shown below) or change the indexing in the script i.e. --> IS(1,tmp)
BM = 560.5;
BS = [0.865 1.025 0 2.230 2.355 0]';
IS = [0 0 1.58 0 0 0]';
FS = [0 2.105 0 1.869 2.835 1.76]';
[RL,HI]=inputt(BM,BS,IS,FS)
function [RL,HI]=inputt(BM,BS,IS,FS)
if nargin < 4
error("4 Inputs required");
end
n=length(BS);
RL(1,1)=BM;
for i=1:n
HI(i,1) = BS(i,1)+BS(i,1);
tmp = i+1;
while tmp<=n && IS(tmp,1)~=0
RL(tmp,1) = HI(i,1)-IS(tmp,1);
tmp=tmp+1;
end
if tmp<=n
RL(tmp,1) = HI(i,1)-FS(tmp,1);
end
i = tmp;
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!