Have a look at the permute! function in julia:
@less permute!([1,2], [1,2])
Modifying it to
function permute!!{T<:Integer}(a, i, p::AbstractVector{T})
count = 0
start = 0
while count < length(a)
ptr = start = findnext(p, start+1)
temp = a[i,start]
next = p[start]
count += 1
while next != start
a[i,ptr] = a[i,next]
p[ptr] = 0
ptr = next
next = p[next]
count += 1
end
a[i,ptr] = temp
p[ptr] = 0
end
a
end
should work (untested). Note that permuting a column should be faster
though as julia matrices are column-major.