MATLAB Answers

Are there any built-in way to calculate inversions?

조회 수: 2(최근 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.

  댓글 수: 0

Sign in to comment.

채택된 답변

David Goodmanson
David Goodmanson 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

Sign in to comment.

추가 답변(0개)

태그


Translated by