Getting the mapping for a permutation in MATLAB?

Up vote 3 down vote favorite share g+ share fb share tw.

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. Matlab link|improve this question edited Mar 28 at 1:33 asked Mar 28 at 0:22roseck1,911418 77% accept rate.

– reve_etrange Mar 28 at 1:24 @reve_etrange : Yes – roseck Mar 28 at 1:32.

How about this: I a = sort(A); I be = sort(B); mapping = b(a).

Speed is about 2x better than ismember. – reve_etrange Mar 28 at 13:09 Somebody else already did all the hard work optimizing sort. :) – Nate Mar 28 at 13:13 Apparently it is just a quick sort (so we know who "somebody" is).

BTW the performance ratio is about the same across >5 orders of magnitude of array sizes. – reve_etrange Mar 28 at 22:32.

Use ismember. ~,idx = ismember(A,B); The vector idx will contain indices such that B(idx) == A. Note that ismember finds the highest indices.

Try this: for i=1:size(B) C(:,i) = I find(B==A(i)) end.

This will cause problems if more than one element of B is equal to an element of A. – reve_etrange Mar 28 at 1:22 and is less efficient than other solutions – yohai Mar 28 at 11:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions