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

2 views (last 30 days)
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)) )

Answers (1)

Cris LaPierre
Cris LaPierre on 2 Mar 2023
Edited: Cris LaPierre on 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 Comments
Cris LaPierre
Cris LaPierre on 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.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!