Help Index exceeds the number of array elements?
Show older comments
The error i am recieving is:
Index exceeds the number of array elements. Index must not exceed 0.
Error Line 39: disp(['Book 1: ', num2str(x(1))]);
I bellive the issues might be occuring in:
Line 34: options = optimoptions('linprog', 'Display', 'off'); or
Line 35 [x, fval] = linprog(f, A, b, [], [], lb, [], options);
But can't find what is causing the error.
Answers (2)
Shubham
on 21 Aug 2024
0 votes
Hi James,
The error message you're encountering, "Index exceeds the number of array elements. Index must not exceed 0," indicates that you're trying to access an element of the array x that doesn't exist. This typically happens if the linprog function fails to find a feasible solution, resulting in an empty x.
Here's a breakdown of how you can troubleshoot and resolve this issue:
Troubleshooting Steps
- Check the Output of linprog:
- Ensure that linprog is returning a valid solution. If it fails to find a feasible solution, x will be empty, which leads to the error when you try to access x(1).
2. Inspect the Inputs to linprog:
- Verify that the inputs to linprog are correctly defined. Check the dimensions and values of f, A, b, lb, and any other constraints you are using. Ensure that they are consistent and correctly set up to define a feasible problem.
3. Check for Feasibility:
- Make sure that the linear programming problem is feasible. If the constraints are too strict or contradictory, linprog will not find a solution. You might want to try running linprog without the options argument initially to see if it provides any useful diagnostic messages.
4. Examine the options Configuration:
- Although unlikely to be the direct cause, double-check the options set in optimoptions. Ensure that they are appropriate for your problem. You can temporarily set 'Display', 'iter' to see more detailed output from the solver, which might help diagnose the issue.
2 Comments
Walter Roberson
on 21 Aug 2024
Also: ask linprog for at least three outputs:
[x, fval, exitflag] = linprog(f, A, b, [], [], lb, [], options);
and check what exitflag is according to https://www.mathworks.com/help/optim/ug/linprog.html#buusznx-exitflag
James
on 21 Aug 2024
Hi James,
The error message you're encountering, "Index exceeds the number of array elements. Index must not exceed 0," indicates that you are trying to access an element in an array that does not exist. Specifically, it seems you are trying to access ‘x(1)’ on line 39, but ‘x’ is either empty or not defined properly.
Here's a demonstration of the issue:
x=[];
x(1)
In this example, ‘x’ is an empty array, so attempting to access ‘x(1)’ results in an error. You might want to check how ‘x’ is being defined or populated in your code to resolve this issue.
Here are some potential reasons and solutions:
- Check the output of “linprog”: Ensure that the “linprog” function is returning a valid solution. If “linprog” fails to find a solution, ‘x’ might be empty. Refer for more details: https://www.mathworks.com/help/optim/ug/linprog.html
- https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.optimoptions.html
- Validate Input Dimensions: Double-check the dimensions of ‘f’, ‘A’, ‘b’, and ‘lb’ to ensure they are consistent and appropriate for linear programming. The size mismatch can lead to “linprog” not returning a valid solution.
- Debugging Steps: MATLAB debugger can be used to inspect the variables and their data types. You can also add debugging statements after the “linprog” call to check the value of output ‘x’. Refer:https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
[x, fval, exitflag, output] = linprog(f, A, b, [], [], lb, [], options);
disp('x:');
disp(x);
exitflag
output
By addressing these points, you should be able to identify why ‘x’ is not being populated correctly and fix the issue.
If the problem persists, consider providing more context or code for further assistance.
Thanks!
2 Comments
f = [-10, -12, -8, -20];
A1 = [3, 8, 2, 0];
b1 = 620 - (2 * 13);
A2 = [50, 70, 80, 0];
b2 = 9500 - 65 * 13;
A3 = [-0.5, 0, 1, 0];
b3 = 0;
A4 = [0, 0, -1, 0];
b4 = -20;
A = [A1; A2; A3; A4];
b = [b1; b2; b3; b4];
lb = [0, 0, 20, 0];
options = optimoptions('linprog', 'Display', 'off');
[x, fval, exitflag, output] = linprog(f, A, b, [], [], lb, [], options);
if exitflag < 0
fprintf('exitflag negative, was: %d\n', exitflag)
else
disp('Optimal number of books to order with updated profit for Book 4:');
disp(['Book 1:', num2str(x(1))]);
disp(['Book 2:', num2str(x(2))]);
disp(['Book 3:', num2str(x(3))]);
disp(['Book 4:', num2str(13)]);
disp(['Maximum Profit: ', num2str(-fval)]);
end
-3 Problem is unbounded.
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!