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

1 vue (au cours des 30 derniers jours)
Andra Cahaya Khalief
Andra Cahaya Khalief le 2 Mar 2023
Commenté : Cris LaPierre le 9 Mar 2023
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)) )

Réponses (1)

Cris LaPierre
Cris LaPierre le 2 Mar 2023
Modifié(e) : Cris LaPierre le 2 Mar 2023
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 commentaires
Andra Cahaya Khalief
Andra Cahaya Khalief le 9 Mar 2023
So how should I write the code?, which section should I fix?
Cris LaPierre
Cris LaPierre le 9 Mar 2023
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by