Cody

# Problem 42920. Find the inverse permutation

Solution 1692837

Submitted on 14 Dec 2018 by Martin C.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
for permlength = 1:100 perm = randperm(permlength); invPerm = inversePermutation(perm); assert(isequal(perm(invPerm),1:permlength)) end

invPerm = 1 B = 1 I = 1 invPerm = 1 invPerm = 1 2 B = 1 2 I = 2 1 invPerm = 2 1 invPerm = 1 2 3 B = 1 2 3 I = 3 2 1 invPerm = 3 2 1 invPerm = 1 2 3 4 B = 1 2 3 4 I = 3 4 2 1 invPerm = 3 4 2 1 invPerm = 1 2 3 4 5 B = 1 2 3 4 5 I = 4 5 3 1 2 invPerm = 4 5 3 1 2 invPerm = 1 2 3 4 5 6 B = 1 2 3 4 5 6 I = 2 3 6 5 1 4 invPerm = 2 3 6 5 1 4 invPerm = 1 2 3 4 5 6 7 B = 1 2 3 4 5 6 7 I = 7 2 4 6 5 3 1 invPerm = 7 2 4 6 5 3 1 invPerm = 1 2 3 4 5 6 7 8 B = 1 2 3 4 5 6 7 8 I = 2 4 3 5 6 1 8 7 invPerm = 2 4 3 5 6 1 8 7 invPerm = 1 2 3 4 5 6 7 8 9 B = 1 2 3 4 5 6 7 8 9 I = 4 6 2 1 8 3 7 9 5 invPerm = 4 6 2 1 8 3 7 9 5 invPerm = 1 2 3 4 5 6 7 8 9 10 B = 1 2 3 4 5 6 7 8 9 10 I = 4 6 9 5 1 10 2 3 8 7 invPerm = 4 6 9 5 1 10 2 3 8 7 invPerm = 1 2 3 4 5 6 7 8 9 10 11 B = 1 2 3 4 5 6 7 8 9 10 11 I = 6 11 10 7 3 4 2 9 8 1 5 invPerm = 6 11 10 7 3 4 2 9 8 1 5 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 B = 1 2 3 4 5 6 7 8 9 10 11 12 I = 12 3 8 11 10 9 7 5 1 4 2 6 invPerm = 12 3 8 11 10 9 7 5 1 4 2 6 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 I = 6 5 3 12 7 8 1 9 10 11 13 4 2 invPerm = 6 5 3 12 7 8 1 9 10 11 13 4 2 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 I = 8 12 11 9 13 2 14 5 6 7 3 10 1 4 invPerm = 8 12 11 9 13 2 14 5 6 7 3 10 1 4 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I = 10 2 8 4 7 1 11 14 3 6 13 5 15 9 12 invPerm = 10 2 8 4 7 1 11 14 3 6 13 5 15 9 12 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 I = 8 9 10 5 2 16 14 13 6 3 7 15 12 1 4 11 invPerm = 8 9 10 5 2 16 14 13 6 3 7 15 12 1 4 11 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 I = 3 6 10 1 8 15 13 12 4 2 17 7 16 9 14 5 11 invPerm = 3 6 10 1 8 15 13 12 4 2 17 7 16 9 14 5 11 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 I = 7 16 15 9 10 18 8 11 17 13 4 1 12 14 6 3 2 5 invPerm = 7 16 15 9 10 18 8 11 17 13 4 1 12 14 6 3 2 5 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 I = 11 2 4 8 16 19 9 17 14 7 12 6 5 15 13 18 1 10 3 invPerm = 11 2 4 8 16 19 9 17 14 7 12 6 5 15 13 18 1 10 3 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 I = 8 4 3 18 2 1 10 17 9 13 6 16 14 11 5 15 7 19 12 20 invPerm = 8 4 3 18 2 1 10 17 9 13 6 16 14 11 5 15 7 19 12 20 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 I = 11 13 21 12 8 16 3 1 9 4 6 17 5 2 15 10 19 7 14 18 20 invPerm = 11 13 21 12 8 16 3 1 9 4 6 17 5 2 15 10 19 7 14 18 20 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 I = 21 13 17 12 3 1 20 22 8 19 15 4 16 6 2 18 14 10 5 7 11 9 invPerm = 21 13 17 12 3 1 20 22 8 19 15 4 16 6 2 18 14 10 5 7 11 9 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 I = 22 3 6 16 10 23 8 17 4 14 2 19 7 11 1 15 9 21 12 20 13 18 5 invPerm = 22 3 6 16 10 23 8 17 4 14 2 19 7 11 1 15 9 21 12 20 13 18 5 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 I = 5 3 20 19 4 23 16 11 12 2 24 13 1 10 18 22 6 17 15 9 14 21 7 8 invPerm = 5 3 20 19 4 23 16 11 12 2 24 13 1 10 18 22 6 17 15 9 14 21 7 8 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 I = 20 19 9 18 17 4 12 10 14 5 24 2 13 11 7 16 23 15 6 3 21 8 22 25 1 invPerm = 20 19 9 18 17 4 12 10 14 5 24 2 13 11 7 16 23 15 6 3 21 8 22 25 1 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 I = 22 20 9 4 3 8 21 23 26 16 15 7 11 5 17 19 13 14 25 1 18 10 24 12 2 6 invPerm = 22 20 9 4 3 8 21 23 26 16 15 7 11 5 17 19 13 14 25 1 18 10 24 12 2 6 invPerm = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 B = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 I = 14 9 13 24 12 21 22 20 5 2 10 19 1 ...