There are some other options you can consider.
1.) (Irrelevant option. Use ibancg's option, cleaner and faster.)
If you have just those two values, a simpler and faster solution would be to initialize to one of these two numbers and flip just a subset of the numbers:
A = [ 10; 25; 25; 10; 25; 25; 10];
X = ones(size(A)) * 10; % initialize X with 10.
X(A == 10) = 25; % Set values where A is 10 to 25.
2.) (irrelevant, too slow): Another possibility if you know that some numbers will never appear is to use temporary third number and avoid creating a new matrix - operate kind of similar to the typical "swap two numbers" recipe.
A(A == 10) = 1; % Temp value
A(A == 25) = 10;
A(A == 1) = 25;
3.) Finally, the third option would be to save logical matrix and then overwrite A.
Ais10 = A==10;
Ais25 = A==25;
A(Ais10) = 25;
A(Ais25) = 10;
Now, for the benchmarks: I used very simple script, varying N and M.
tic
for i = 1 : M
X1 = randi([1 2],N,1);
... % depends on test cases. Result can be in X or A.
toc
Therefore, it also times randi, but as it is a part of every code it should only mask the relative speed differences, keeping absolute ones the same. Computer is i7 4770k, 32GB RAM.
Row vector has 1000 elements, 1M runs; 100M elements, 10 runs; 1B elements, 1 run; 3B elements, 1 run
Codes that rely on having just 2 values:
- 1I): 18.3 s; 20.7 s; 20.3 s; 63.6 s
- 1Z): 33.0 s; 38.2 s; 38.0 s; NA (out of memory)
Code relies on lacking one value that can be used for swap:
- 2Z): 54.0 s; 60.5 s; 60.0 s; 659 s
Code handles arbitrary data, swapping 2 values.
- 3A): 45.2 s; 50.5 s; 49.0 s; NA (out of memory)
- 3Z): 41.0 s; 46.1 s; 45.8 s; NA (out of memory)
So, to summarize, Ibancg's option is much faster and simpler than the rest and should be used if possible (= just those 2 different values in the vector). My 2nd option is far too slow to be relevant. Rather do step by step swap on the vector using any of the general methods. Either general option is fine.