if x(:,3)>x(:,4) really does not work, if expects either true or false not a vector. Therefore, it only evaluates the first element of the vector x(:,3)>x(:,4) , so it ignores your elseif .
So, you should either use a loop, or even better, use logical indexing as follows:
x= [10 5 1 2 10 5 2 1 NaN 1 1 3] output = NaN(size(x,1),1) I = x(:,3)>x(:,4); output(I) = x(I,1)-x(I,2); I = x(:,3)<x(:,4); output(I) = x(I,2)-x(I,1)
Dan
source share