0

Are there any direct function to switch elements i,j in any list? or is necessary to create one with Do / If ? (I don´t ask for making me this function if it doesn´t exists)

My target is can switching positions i,j in {1,4,3,2,5} for example I tell switch(2,5) an obtain {1,5,3,2,4}

Mika Ike
  • 3,241
  • 1
  • 23
  • 38
  • 3
    mylist[[{i,j}]]=mylist[[{j,i}]] – ciao Apr 19 '14 at 06:18
  • @rasher then... to obtain a copy with the switched elements, I need to make a new copy, and use a line like you posted. or are there a direct way to create a copy with elements i,j switched? – Mika Ike Apr 19 '14 at 06:23
  • @rasher For me, your comment solved my initial post. How could I put SOLVED? – Mika Ike Apr 19 '14 at 06:30
  • 1
    If you don't want to change the original list (which my comment does), first copy it, then do the swap on the copy. No shorter way I know of. No need to mark "solved" - this is pretty trivial, question will probably just get closed. – ciao Apr 19 '14 at 06:48
  • ok @rasher, then.... If you don´t mind, I delette it, I don´t want problems, and continue learning in this forum ;-)

    Thank you

    – Mika Ike Apr 19 '14 at 06:49
  • 1
    If you do not want to type the reverse list, you can try m = {1, 4, 3, 2, 5}; idx = {1, 2, 4}; m[[idx]] = m[[Reverse[idx]]]; I also do not know why you need to save the actual data in the position affected. You have the index of the positions switched there. So you can always obtain the data that was switched anytime. Save memory ! Just do oldData = m[[Reverse[idx]]] to read the old data – Nasser Apr 19 '14 at 06:52
  • @nasser I need to save date to compare objective functions with diferent permutations. Thank you for your appreciation – Mika Ike Apr 19 '14 at 06:58

0 Answers0