Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the mapping for a permutation in MATLAB

Tags:

matlab

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)

Update:

Is there a fast vectorized solution that does not use ismember ? In my experience, ismember tends to be slow for very large arrays.

like image 299
Amelio Vazquez-Reina Avatar asked Mar 28 '12 00:03

Amelio Vazquez-Reina


People also ask

How do you find the permutation matrix in Matlab?

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.

How do you get all possible combinations in Matlab?

C = combntns( v , k ) returns all possible combinations of the set of values v , given combinations of length k .

How do you find the number of elements in an array in Matlab?

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .


1 Answers

How about this:

[i a] = sort(A);
[i b] = sort(B);
mapping = b(a)
like image 105
Nate Avatar answered Sep 28 '22 11:09

Nate