Index exceeds matrix dimensions problem with this code below :-

조회 수: 17 (최근 30일)
hannibal lecter
hannibal lecter 2018년 1월 7일
답변: Walter Roberson 2018년 1월 7일
c =
1 1 1 6
2 4 2 16
-1 5 -4 -3
N = length(c);
tol= 1e-6;
for ii=1:N-1
for jj=ii+1:N
if abs(c(jj,ii))> tol
fac=c(ii,ii) / c(jj,ii);
c(jj,:) = fac*c(jj,:) - c(ii,:);
end
end
end
how to solve this problem? that code for Gauss elimination when I press enter it appear " Index exceeds matrix dimensions "

채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2018년 1월 7일
c =[1 1 1 6; 2 4 2 16; -1 5 -4 -3]
N = size(c,1)
M=size(c,2)
tol= 1e-6;
for ii=1:M-1
ii
for jj=ii+1:N
jj
if abs(c(jj,ii))> tol
fac=c(ii,ii) / c(jj,ii);
c(jj,:) = fac*c(jj,:) - c(ii,:);
end
end
end
Instead of writing length(c), you should to define the size along first and second dimension of matrix, i.e. define the number of row and columns of matrix.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 7일
length() of a 3 x 4 array is 4. length() is not the number of rows: length() is defined as:
  • 0 if any dimension of the array is 0; or
  • max() of size() of the array otherwise.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by