Index in position 2 exceeds array bounds. Index must not exceed 5

조회 수: 148 (최근 30일)
hello evreyone I need some help :)
here is my code. the error is in this line, line 16%maksimum=max(abs(U(i:end,i))); I used this matrix to test the code [1 2 3 4 1;2 4 6 8 2;3 6 9 12 3;4 8 12 16 4;8 6 4 2 8;4 3 2 1 4], the code works for some matrices but not for all. I want it to work like matlab's lu() function :) but it gave me some errors like "Index in position 2 exceeds array bounds. Index must not exceed 5."
% This code decomposes matrix A into upper and lower triangular matrices.
clear;
clc;
disp('Matlab code for LU decomposition of a matrix using ''gaussian elimination''and ''partial pivoting''')
disp('Click Enter to continue.')
pause
clc;
A = input("enter a matrix: ");
tic
[n,m]=size(A);
U=A;%upper triangular matrix
L=eye(n);%lower triangular matrix
P=eye(n);%permutation matrix
for i = 1:n
if true %partial pivoting
maksimum=max(abs(U(i:end,i)));
for k=1:n
if maksimum == abs(U(k,i))
U([i k],:) = U([k i],:);
P([i k],:) = P([k i],:);
end
end
end
for j = i+1:n
L(j,i)=U(j,i)/U(i,i);
U(j,:) = U(j,:)-U(j,i)*U(i,:)/U(i,i);
end
end
disp('permutation matris(P):')
disp(P)
disp("A'in ust ucgen matris(U):")
disp(U)
disp("A'in alt ucgen matris(L):")
disp(L)
toc

채택된 답변

Adam Danz
Adam Danz 2022년 3월 27일
편집: Adam Danz 2022년 3월 30일
Let's look at the line that's reported to be causing the problem and the error message.
maksimum=max(abs(U(i:end,i)));
Index in position 2 exceeds array bounds. Index must not exceed 5
  1. "Index in position 2" means the 2nd index value in u(i:end, i) which is the last i. That i is causing a problem.
  2. "Index in position 2 exceeds array bounds" means that the last i is larger than the array size. Position 2 indexes the columns of an array. So at some point i becomes larger than the number of columns in U.
  3. "Index must not exceed 5" : Matlab tells us that U has 5 columns so the error happens when i==6.
Now that we understand the error message, how do we fix it? This is where your expertise comes in as the owner of the code. Here are some possible problems:
  • Maybe U isn't the size it should be. Maybe it should contain more than 5 columns.
  • Maybe U is the right size but the indexing is the problem. Maybe i shouldn't ever be >5, or, greater than the number of columns in U.
  • The problem might be caused at this line where n is the number of rows, not columns [n,m]=size(A);
  • But this part also looks fishy: U(i:end,i); is that really what you want to do (I don't know), to index rows i:end of column i?
  댓글 수: 1
Mohammed Izedin Mohammed
Mohammed Izedin Mohammed 2022년 3월 27일
you are right I solved it by changing the index n to min(n,m) in the first for loop :) thnaks a lot
% This code decomposes matrix A into upper and lower triangular matrices.
clear;
clc;
disp('Matlab code for LU decomposition of a matrix using ''gaussian elimination''and ''partial pivoting''')
disp('Click Enter to continue.')
pause
clc;
A = input("enter a matrix: ");
tic
[n,m]=size(A);
U=A;%upper triangular matrix
L=eye(n);%lower triangular matrix
P=eye(n);%permutation matrix
for i = 1:min(n,m)
if true %partial pivoting
maksimum=max(abs(U(i:end,i)));
for k=1:n
if maksimum == abs(U(k,i))
U([i k],:) = U([k i],:);
P([i k],:) = P([k i],:);
end
end
end
for j = i+1:n
L(j,i)=U(j,i)/U(i,i);
U(j,:) = U(j,:)-U(j,i)*U(i,:)/U(i,i);
end
end
disp('permutation matris(P):')
disp(P)
disp("A'in ust ucgen matris(U):")
disp(U)
disp("A'in alt ucgen matris(L):")
disp(L)
toc

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by