MATLAB: Is there a faster alternative for find() operation

findindexingparfor

I have two large vectors X and Y holding indices. Lets say
X = [1; 2; 3; 4; 1; 4;......]
Y = [10; 21; 4; 4; 10; 7;......]
I want to get the indices for a given values of a and b, like for example for a = 1 and b = 10, I must get the indices 1 and 5.
I could do that with the use of find() as follows
parfor a = 1:N
for b = 1:M
Indices_list{a,b} = find(and((X == a), (Y == b)));
end
end
In spite of using parfor, it it still taking a lot of time. Please help me if there is a faster way to achieve this.

Best Answer

This can be done in just one line without any loop using accumarray:
X = [1; 2; 3; 4; 1; 4];
Y = [10; 21; 4; 4; 10; 7];
Indices_List = accumarray([X, Y], (1:numel(X))', [], @(idxlist) {idxlist})
If you do want an M x N array and M or N is different from max(X) or max(Y) respectively, then replace the [] by [M, N].