Since you care about the distance from any element to any element, you can create a matrix of distances from vectorized matrices and measure it for a distance threshold. Example:
A = rand(10, 4); % (example) matrix AB = rand(3, 5); % matrix B of different size minDist = 0.005;
Solution . Repeat vectorized matrices, columns and rows to get matrices of the same size and apply a distance estimate based on the matrix:
Da = repmat(A(:), 1, length(B(:))); % size 40 x 15 Db = repmat(B(:)', length(A(:)), 1); % size 40 x 15 DD = Da - Db; indA = any(abs(DD) < minDist, 2);
Using any() will give you logical indices for any value of A that is close to any value of B ). You can directly index / return elements of A using indA.
The DD matrix (as @Shai also points out) can be equivalently evaluated via bsxfun
DD = bsxfun(@minus, A(:), B(:)');
In addition : you can map from the index of the row (corresponding to the elements of A) back to the matrix A with:
[iA, jA] = ind2sub(size(A), indA);
Assert / test that all returned values ββare less than minDist , for example. using:
for k = 1:length(iA); d(k) = min(min(abs(A(iA(k), jA(k)) - B))); end all(d < minDist)
(tested in Octave 3.6.2)