Index in position 2 exceeds array bounds appeared in code, how to fix it?

조회 수: 1 (최근 30일)
Andra Cahaya Khalief
Andra Cahaya Khalief 2023년 3월 2일
댓글: Cris LaPierre 2023년 3월 9일
function [xs, cost, A3] = f_bigM(A,M,n)
nc = 1;
A3(:,:,nc) = A;
%rA = size(A,1);
cA = size(A,2);
% initial problem
j_M = find(A(end,:)==M);
for jj = j_M
ii = find(A(:,jj)==1);
k = -A(end,jj)/A(ii,jj);
A(end,:) = A(end,:)+k*A(ii,:);
end
nc = nc+1;
A3(:,:,nc) = A;
% find basic variables
i_res = (1);
for ii=1:cA
a = A(:,ii);
if(norm(a)==sum(a))
jj = a==1;
i_res = i_res(ii,jj);
end
end
soln_exists = 0;
if ( length(unique(i_res(:,2))) == length(i_res(:,2)) )
xs = zeros(1,cA-1);
xs(i_res(:,1)) = A(i_res(:,2),end);
soln_exists = isempty(find(xs < 0, 1));
end
xs = []; cost = [];
if soln_exists
[xs, cost, A3s] = f_simplex(A,n);
ns = size(A3s,3);
A3(:,:,end+1:end+ns) = A3s;
end
Error in line :
if ( length(unique(i_res(:,2))) == length(i_res(:,2)) )

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 3월 2일
편집: Cris LaPierre 2023년 3월 2일
This error is a result of using an index that exceeds the size of your array dimension (in this case, the 2nd dimension, or columns). It would appear your variable i_res does not have a 2nd column to index.
a = (1:5)'
a = 5×1
1 2 3 4 5
% There is no second column, so an error message is displayed.
a(:,2)
Index in position 2 exceeds array bounds. Index must not exceed 1.
  댓글 수: 2
Andra Cahaya Khalief
Andra Cahaya Khalief 2023년 3월 9일
So how should I write the code?, which section should I fix?
Cris LaPierre
Cris LaPierre 2023년 3월 9일
I'd start with the line containing the error:
if ( length(unique(i_res(:,2))) == length(i_res(:,2)) )
You haven't shared your variables with us, so all we can do is tell you that 2 does not appear to be a valid index. Try using 1 instead.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by