I need your help to describe the following matlab code in a scientific way:
function [y,x]= select1()
x=10*rand(1,7);
m=[3 7];
dif1=abs(m(1)-x);
dif2=abs(m(2)-x);
if (sum(dif1(:)<dif2(:)) >
sum(dif1(:)>dif2(:)));
y= m(1);
else
y= m(2);
end
I understand that this function selects m1 if most of x values are closer to m1 than to m2, and selects m2 else, but I am having a hard time describing its steps properly with mathematical notation.
It is a binary choice.
Here is a more concise and general way to write it
You can now add arbitrary many values into m and it will still work.
You might need to use repmat to get it to work in older versions of Matlab which have not adopted Octaves "automatic broadcasting".