Are there any built-in way to calculate inversions?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ivor Horton
am 15 Jun. 2020
Beantwortet: David Goodmanson
am 16 Jun. 2020
In Mathematica, there is a built-in function
Inversions
which counts the number of inversions in permutation.
See the document for details at https://reference.wolfram.com/language/Combinatorica/ref/Inversions.html
Is there a similar function in Matlab?
Thanks.
0 Kommentare
Akzeptierte Antwort
David Goodmanson
am 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) = [];
end
inversions % the result
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!