MATLAB Answers

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

3 302 views (last 30 days)
noe
noe on 21 Nov 2018
Commented: Jan on 9 Dec 2019
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 "am(in,in)=bm(in,im);". What does "Index in position 1 exceeds array bounds (must not exceed 1)" mean? and how to solve this?
Here is code :
n=3
a=[1,2,3;4,3,2;,9,8,7]
b=[3;4;2]
k=n;
for i=1:k
for j=1:k+1
if j~=k+1
am(i,j)=a(i,j);
else
am(i,j)=b(i);
end
end
end
m=n+1;
for i=1:n
for j=1:m
bm(i,j)=am(i,j)/am(i,i);
end
for k=1:n
for l=1:m
if i==k
bm(k,l)=am(k,l)-bm(i,l)*am(k,i);
end
end
end
for in=1:n
for im=1:m
am(in,im)=bm(in,im);
end
end
end
for i=1:n
c(i)=am(i,m);
end
  2 Comments

Sign in to comment.

Answers (2)

Jan
Jan on 21 Nov 2018
Edited: Jan on 21 Nov 2018
The message:
"Index in position 1 exceeds array bounds (must not exceed 1)"
seems to be very clear: The first index of an array is 2, but the array has a length of 1 in the first dimension only. Use the debugger to examine such problems:
dbstop if error
Type this in the command window and run the code again. When Matlab stops at the error, cehck the sizes of the used variables:
size(bm)
in
im
By the way, this can be simplified:
for i=1:k
for j=1:k+1
if j~=k+1
am(i,j)=a(i,j);
else
am(i,j)=b(i);
end
end
end
to:
am = [a(1:k, 1:k), b];
% Or:
am = [a, b];

Gul Rukh Khan
Gul Rukh Khan on 8 Dec 2019
Respected Sir,
I have one question in my 10x10 matrix image.
I want to compare first pixel of 10x10 matrix with the right side and bottom side element, and based on the difference whichever is greater move towards that direction.
This is bacially for Edge Detection, sir.
Can you help me accordingly. i am waiting for your prompt response. Thanks
best Regards
gul Rukh Khan
  1 Comment
Jan
Jan on 9 Dec 2019
Please do not attach a new question to an existing thread. Open a new thread instead and remove this message. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!