# Index in position 2 exceeds array bounds (must not exceed 8)

조회 수: 8(최근 30일)
Surya Ramanthan 2020년 10월 20일
댓글: Rik 2020년 10월 21일
I keep getting the error "Index in position 2 exceeds array bounds (must not exceed 8) with the following code. I am trying to determine whether a matrix is strictly diagonally dominant or not. The error appears on the line with sumrow=sumrow+abs(A(i,j));
This is the code.
A = [28 -6 4 1 -2 -5 8 0;
-4 28 -1 4 0 4 4 6;
1 -6 26 -5 1 -1 -6 0;
1 -6 26 -5 1 -1 -6 0;
-5 -6 1 21 0 -3 2 2;
4 1 3 -3 17 0 -3 3;
4 -2 3 0 2 14 0 1;
1 2 4 3 -2 1 17 4;
1 3 1 3 0 -1 3 15];
rowcol=size(A);
n=rowcol(1);
count=0;
for i=1:1:n
sumrow=0;
for j=1:1:n
if i~=j
sumrow=sumrow+abs(A(i,j));
end
end
if abs(A(i,i))>sumrow
count=count+1;
end
end
if count==n
disp('Matrix is strictly diagonal dominant')
else
disp('Matrix is NOT strictly diagonal dominant')
end
Any help with corrections will be greatly appreciated.
##### 댓글 수: 1표시숨기기 없음
Rik 2020년 10월 21일
Regarding your flag: what would make this question a duplicate?
Just in case you will try to edit away your question: I made a capture to the Wayback Machine, so everyone with editing priviliges can put your question back the way it is now.

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

### 채택된 답변

The matrix is not square, so you need to find the minimum of row and column numbers.
A = [28 -6 4 1 -2 -5 8 0; -4 28 -1 4 0 4 4 6; 1 -6 26 -5 1 -1 -6 0; 1 -6 26 -5 1 -1 -6 0; -5 -6 1 21 0 -3 2 2; 4 1 3 -3 17 0 -3 3; 4 -2 3 0 2 14 0 1; 1 2 4 3 -2 1 17 4; 1 3 1 3 0 -1 3 15];
rowcol=size(A);
n=min(size(A));
count=0;
for i=1:1:n
sumrow=0;
for j=1:1:n
if i~=j
sumrow=sumrow+abs(A(i,j));
end
end
if abs(A(i,i))>sumrow
count=count+1;
end
end
if count==n
disp('Matrix is strictly diagonal dominant')
else
disp('Matrix is NOT strictly diagonal dominant')
end

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

### 추가 답변(1개)

Cris LaPierre 2020년 10월 20일
편집: Cris LaPierre 2020년 10월 20일
The issue I see is that you are assuming A is square (8x8), but it is not. It has dimensions 9x8. However, you are only using the first number, 9, to loop through the rows and columns of A. This is causing an error in your second for loop (j=9).
Try
...
for i=1:size(A,1)
...
for j=1:size(A,2)
...
end
...
end
...
##### 댓글 수: 1표시숨기기 없음
Image Analyst 2020년 10월 20일
Plus you should really be more general like this:
[rows, columns] = size(A);
count=0;
for row = 1 : rows
sumrow = 0;
for col = 1 columns
etc. The above code does not depend on the number of rows matching the number of columns. If you need them to match, you should check for that in advance of the for loop:
[rows, columns] = size(A);
if rows ~= columns
errorMessage = sprintf('Error : rows and columns do not match. Rows = %d while columns = %d', rows, columns);
uiwait(errordlg(errorMessage));
return; % Quit this function/module.
end
for row = 1 : rows
sumrow = 0;
for col = 1 columns

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

### 범주

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by