Index in position 2 exceeds array bounds appeared in code, how to fix it?
2 次查看(过去 30 天)
显示 更早的评论
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)) )
0 个评论
回答(1 个)
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)'
% There is no second column, so an error message is displayed.
a(:,2)
2 个评论
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 Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!