A small case can help you understand -
the code
%%
a1 = rand(10,5); %%
%
element = a1(4,5)+0.00001; %%
%%
[~,ind] = min(reshape(abs(bsxfun(@minus,a1,element)),numel(a1),[]));
%%
[x,y] = ind2sub(size(a1),ind)
Output
x =
4
y =
5
As you can see, the result corresponds to the expected answer.
:. , , bsxfun. -
%%
a1 = rand(10,5); %%
%
search_array = [a1(4,5)+0.00001;a1(6,5)+0.00001;a1(4,4)+0.00001;a1(4,2)+0.00001];
%%
[~,ind] = min(abs(bsxfun(@minus,a1(:),search_array')));%//'
%%
[x,y] = ind2sub(size(a1),ind)
x =
4 6 4 4
y =
5 5 4 2