MATLAB: Question about min and if function

min and if function

Dear All
I have a matrix of three column as below like the file provided, I would like to find the values from o or p columns which is the closest to q, I have written this code:
M=abs(o - q);
N=abs(p - q);
difference =[M N];
[~,ii] = min(difference,[],2);
if ii == 1
Result = o
else
Result = p
end
but it just return the p column to me, I am really confused about the results. I really appreciate your helps
Best Nima

Best Answer

hola trabajé un poco en tu pregunta y lo solucioné de la siguiente manera,
%%Import the data
datos=xlsread('test1.xls','Sheet1','A2:D12');
%%Create output variable
q = datos(:,1);
o = datos(:,3);
p = datos(:,4);
M = abs (o - q);
N = abs (p - q);
R=M>N
result=o.*(1-R)+p.*(R)
saludos desde chile