Swapping two elements in a vector/matrix is very simple in Octave:
x='abcde';
x([4,1])=x([1,4])
x =
dbcae
Unfortunately, I have yet to find a way to do this inside an anonymous function. While the function below is syntactically correct, it only gives back the two elements that are swapped, not the entire x
vector:
f=@(x)(x([4,1])=x([1,4]))
f(x)
ans =
ad
Is there a way to achieve the desired behavior using anonymous functions in a golfy way? Can several elements be swapped this way using an anonymous function?
I could create an indexing vector: f=@(x)x([4,2,3,1,5])
, but creating such a vector dynamically will likely take a lot of bytes too.