If you really are swapping two array elements, work directly with the array rather than (presumably) first generating the full swapped index vector and then using that to permute the array. For example:
sw=: <@~...@[ C. ] a=: 'abcdefghij' 3 5 sw a abcfedghij 3 9 sw a abcjefghid 3 3 sw a abcdefghij In some special situations there are shorter alternatives. e.g. swap the last two items: 1 A. a abcdefghji 1 A. a,'KLMN' abcdefghijKLNM ----- Original Message ----- From: John Randall <[email protected]> Date: Sunday, May 10, 2009 14:36 Subject: [Jprogramming] Swapping array elements To: J-programming forum <[email protected]> > I want to swap two array elements, and I am trying to do it by > transposing indices. So far I have come up with > > swapindices=:}: @: C. @: (; ~.) > > 10 swapindices 3 5 > 0 1 2 5 4 3 6 7 8 9 > 10 swapindices 3 9 > 0 1 2 9 4 5 6 7 8 3 > 10 swapindices 3 3 > 0 1 2 3 4 5 6 7 8 9 > > Is this a sensible approach, or I am missing something much simpler? ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
