I have a list of the form list1 = {1, 3, 4, 2, 5, 6}
and I would like to replace each of the elements in the position of represented by the previous number in the list, for instance replace 1 in position 6, 3 in position 1, 4 in position 3, 2 in position 4 and so on. In the end I should get a list of the form {3, 5, 4, 2, 6, 1}
. I am trying to do the following:
A = ConstantArray[0, Length[list1]]
NewList =
Table[
ReplacePart[
A,
(If[Position[list1, i] - 1 != 0,
Position[list1, i] - 1,
Position[List1, Last[List1]]) -> i],
{i, Range[Length[List1]]}]
I am using If
inside ReplacePart
to consider the case when an element is in position 1 and send it to the position of the last element of the permutation. However, this methos is not working. Could anyone tell me why? Is there is a more efficient method to solve this problem?
If
expression in your code seems to be missing a]
at its end. $\endgroup$ – m_goldberg Jun 19 '16 at 14:13