Index in position 2 exceeds array bounds (must not exceed 1).
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Karim
il 25 Ago 2022
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 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!