sort overloads sort operator


[N,location] = sort(X)


The following silly example defines a regression problem with the constraint that the sum of the largest value and the smallest value of the decision variable is 1. As usual, we add explicit bound constraints to improve the big-M reformulations.

A = randn(20,5);
b = randn(20,1)*20;
x = sdpvar(5,1);

e = b-A*x;
[s,location] = sort(x);
F = [s(1)+s(end) == 1, -100 <= x <= 100];

Note that we can solve this particular problem more efficiently by using max and min instead (still a nonconvex problem, but it leads to a model with 10 binary variables, compared to the sort model which leads to 25 binary variables.)

F = [max(x) + min(x) == 1, -100 <= x <= 100];


The operator is implemented using a mixed-integer model. based on big-M approach, hence it is crucial that all involved variables have explicit bound constraints.

The integer model for the sort operator is extremely expensive. Sorting a variable of n variables requires n^2 binary variables.

Before you use sort, make sure to first check if you can use the convex operators sumk or sumabsk.

Leave a Comment