MATLAB Answers

Are there any built-in way to calculate inversions?

3 views (last 30 days)
In Mathematica, there is a built-in function
which counts the number of inversions in permutation.
Is there a similar function in Matlab?


Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 16 Jun 2020
Hi Ivor,
I do not know of one, but here is one way to accomplish it
p = [3 6 1 4 5 2]; % for example
inversions = 0;
for k = length(p):-1:2
f = find(k==p);
inversions = inversions + k -f;
p(f) = [];
inversions % the result


Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by