I want to select two random columns of a matrix and swap them. I use:
S(:,[round_i round_j]) = S(:,[round_j round_i]);
But my code seems to produce the same matrix as before. Below are a code snippet and output of the command window.
function swapped_schedule=swapRounds(S)
global weeks;
round_i=randi(weeks)
round_j=randi(weeks)
while round_j~=round_i
round_j=randi(weeks);
end
S(:,[round_i round_j]) = S(:,[round_j round_i]);
swapped_schedule=S;
end
Schedule is the matrix I pass to the function swapRounds()
. The output is shown as follows:
schedule =
4 -4 -6 5 -2 6 2 3 -5 -3
5 -6 -4 4 1 3 -1 -5 -3 6
-6 5 -5 6 4 -2 -4 -1 2 1
-1 1 2 -2 -3 5 3 -6 6 -5
-2 -3 3 -1 -6 -4 6 2 1 4
3 2 1 -3 5 -1 -5 4 -4 -2
round_i =
4
round_j =
6
ans =
4 -4 -6 5 -2 6 2 3 -5 -3
5 -6 -4 4 1 3 -1 -5 -3 6
-6 5 -5 6 4 -2 -4 -1 2 1
-1 1 2 -2 -3 5 3 -6 6 -5
-2 -3 3 -1 -6 -4 6 2 1 4
3 2 1 -3 5 -1 -5 4 -4 -2
How do I get this code to swap my two columns?