# sort

Tags:

Updated:

### Syntax

[N,location] = sort(X)


### Examples

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];
optimize(F,norm(e,1));


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];
optimize(F,norm(e,1));