Are there any built-in way to calculate inversions?

3 次查看(过去 30 天)
In Mathematica, there is a built-in function
Inversions
which counts the number of inversions in permutation.
Is there a similar function in Matlab?
Thanks.

采纳的回答

David Goodmanson
David Goodmanson 2020-6-16
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) = [];
end
inversions % the result

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by