Index in position 2 exceeds array bounds (must not exceed 1).

조회 수: 5 (최근 30일)
Md. Shahriar Rahman Sakib
Md. Shahriar Rahman Sakib 2022년 8월 25일
편집: Walter Roberson 2022년 8월 25일
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

답변 (1개)

Karim
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)
RL = 6×1
560.5000 -0.3750 0.4700 -1.8690 1.6250 2.9500
HI = 6×1
1.7300 2.0500 0 4.4600 4.7100 0
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by