MATLAB: Using FIND for common elements WITHOUT a loop (Vectorized?)

findismemberno_loop

Hello,
I have an array A 100×1 with different datenum that
I need to find in a big array B 5000000×1 (all datenum in B are in A, just repeated many times)
and then replace w another datenum C 100×1 (same index as A)
I am using find( ) but it is taking too long. Is there a way to do this without a loop?
BB=B %making a copy of B to check after.
for i=1:size(A,1)
loc=find(B==A(i))
BB(loc)=C(i);
end

Best Answer

Use ismember and its second output argument:
>> A = randperm(5)
A =
3 5 4 1 2
>> B = randi(5,1,9)
B =
5 1 2 5 1 2 4 5 4
>> C = (1:5).^2
C =
1 4 9 16 25
>> [~,X] = ismember(B,A);
>> BB = C(X)
BB =
4 16 25 4 16 25 9 4 9