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 Center および File ExchangeCreating 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