It is possible to sort the data and check the the unique values. This seems to be about as efficient as using the function unique()
. Possibly with an advantage for using sort()
and diff()
. This may however be dependent on hardware and the difference is fairly insignificant, taking into account the simplicity of D = unique([A;B;C]);
.
function test()
% A=[1;2;5;9;15];
% B=[2;3;5;11;15];
% C=[5;7;11;20;25];
A = 500*rand(10000000,1);
B= 500*rand(10000000,1);
C = 500*rand(10000000,1);
f1 = @() testA(A,B,C);
f2 = @() testB(A,B,C);
time1 = timeit(f1,1);
time2 = timeit(f2,1);
disp(time1);
disp(time2);
function D = testA(A,B,C)
d = sort([A;B;C]);
idx = diff(d);
D = d([1;idx]>0);
function D = testB(A,B,C)
D = unique([A;B;C]);
test
1.9085
1.9968