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

1.186 views (last 30 days)
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
Torsten
Torsten on 21 Nov 2018
The array "bm" has size (1,4) when you enter the nested for loop
for in=1:n
for im=1:m
am(in,im)=bm(in,im);
end
end
Thus for in>1, you try to access elements in "bm" that do not yet exist.
Since I did not try to understand your code, you have to solve this problem on your own.

Sign in to comment.

Answers (5)

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
  2 Comments
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.


NOR AZIERA
NOR AZIERA on 14 Dec 2022
Edited: NOR AZIERA on 14 Dec 2022
Sir...can i ask u ..how to solve this problem?
n=3; A=randn(n); A=A'*A; [V,D]=eig(A);
for i=1:n
X=outprod(V(:,i),D(i))*V'
display(X);
display(A');
end
  2 Comments
Jan
Jan on 14 Dec 2022
@NOR AZIERA: Please do not post a new question in the section of answers of another question.
You have asked this in another question already, so please delete this answer.
Rahul Pandit
Rahul Pandit on 26 Dec 2022
Hi Sir
I need your help develop a code for cross correlation using the delay.
I have two 5Sec signals and i want to do the cross corelation of both the signals.
a = xlsread(signals 5sec.xls);
t1 = a(:,1); %time (Time is same for both the signal)
yR1 = a(:,2); % 1st signal
yR2 = a(:,4); % 2nd signal
% Cross Correlation
[r,lags] = xcorr(yR1,yR2);
plot(lags,r,'color','r','linewidth',1.6);
But i want to do the delay of 1 sec and then do the cross correlation using the for loop, i must to iterate the signals 1sec to 5sec for 1st signal and same thing i have to repeate in negative direction also. so i can get the neagtive and positive both the pulses.
Please help me to grow my self.

Sign in to comment.


fatma
fatma on 25 Dec 2022
code:
Ncyc = Ncyc_primes (find (gcd (Ncyc_primes, N) ==1), 1, 'last ')
erorr:
Index in position 1 exceeds array bounds. Index must not exceed 1.
  1 Comment
Jan
Jan on 26 Dec 2022
This line cuases the same error message, but has no other connection to the original error message. Most of all, it is not an answer, but a new question, I assume. So please open a new question and delete the message here. Thanks.

Sign in to comment.


Somad
Somad on 11 Oct 2023
could someone help me please,
this is my loop:
for tdz = 1:length(t_vec)-1 %%%integrator integrates to tdx+1
for idz = 2:length(z_vec)-1 % because 1 is T_mat(1,:), bc, doesnot change, also length(x_vec) = T_mat(end,:)
CCO2v(idz, tdz+1) = ( CCO2v(idz,tdz)+ (-CCO2v(idz,tdz+1) * (uG(idz+1,tdz) - uG(idz-1,tdz))/dz - uG * (CCO2v(idz+1,tdz) - CCO2v(idz-1,tdz))/dz - aw*NCO2)*dt)/gasholdup;
CCO2l(idz, tdz+1) = (CCO2l(idz,tdz) - (uL*(CCO2l(idz-1,tdz) - CCO2l(idz+1,tdz))/dz + aw*NCO2)*dt)/liqholdup;
TGmat(idz, tdz+1) = ((-uG*(TGmat(idz+1,tdz) - TGmat(idz-1,tdz))/dz + (aw/(CCO2v(idz, tdz+1)*cp_CO2))*hg*(TLmat(idz, tdz+1)-TGmat(idz, tdz+1)))*dt + TGmat(idz,tdz))/gasholdup;
TLmat(idz, tdz+1) = ((-uL*(TLmat(idz-1,tdz) - TLmat(idz+1,tdz))/dz - aw*hg(TLmat(idz, tdz+1)-TGmat(idz, tdz+1))/(CCO2v(idz, tdz+1)*cp_CO2) - dH*NH2O - dHH2O*NH2O)*dt + TLmat(idz,tdz))/liqholdup;
end
end
the error also: Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in Absorber (line 235)
CCO2v(idz, tdz+1) = ( CCO2v(idz,tdz)+ (-CCO2v(idz,tdz+1) * (uG(idz+1,tdz) - uG(idz-1,tdz))/dz - uG * (CCO2v(idz+1,tdz) - CCO2v(idz-1,tdz))/dz - aw*NCO2)*dt)/gasholdup;
  1 Comment
Torsten
Torsten on 11 Oct 2023
Edited: Torsten on 11 Oct 2023
How did you initialize the 2d-array CCO2v ?
On the right-hand side of the equation
CCO2v(idz, tdz+1) = ( CCO2v(idz,tdz)+ (-CCO2v(idz,tdz+1) * (uG(idz+1,tdz) - uG(idz-1,tdz))/dz - uG * (CCO2v(idz+1,tdz) - CCO2v(idz-1,tdz))/dz - aw*NCO2)*dt)/gasholdup;
you try to access elements of CCO2v that do not yet seem to exist.
Further, the right-hand side contains CCO2v(idz,tdz+1) which you want to define by the equation. This must be wrong.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!