Say I have two arrays where one is a permutation of the other:
A = [2 1 5 3 7]
B = [7 2 1 3 5]
with no repetitions in either array.
How can I obtain the permutation mapping between both?
E.g. A->B
should be:
[2, 3, 5, 4, 1]
which means:
A(1) -> B(2)
A(2) -> B(3)
A(3) -> B(5)
A(4) -> B(4)
A(5) -> B(1)
Is there a fast vectorized solution that does not use ismember
? In my experience, ismember
tends to be slow for very large arrays.
P = perms( v ) returns a matrix containing all permutations of the elements of vector v in reverse lexicographic order. Each row of P contains a different permutation of the n elements in v . Matrix P has the same data type as v , and it has n! rows and n columns.
C = combntns( v , k ) returns all possible combinations of the set of values v , given combinations of length k .
n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .
How about this:
[i a] = sort(A);
[i b] = sort(B);
mapping = b(a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With