MATLAB: How to define if or for for the following question

forif statementmatrix

I have matrix x=[1 2 4 5] and matrix y= [3 4 2 4], i wanna find if x(i,j)-y(i,j)<0 put highest value between that (i,j) in a new matrix, and if x(i,j)-y(i,j)>0 put the lowest value between that (i,j) in a new matrix, how can i do that? for example here: x-y= [-2 -2 2 1] because the first row and column of x-y in less than zero (-2), as a result in new matrix (like d) the first row and column must be the highest value between x(1,1) and y(1,1) and so on…,matrix d here would be d=[3 4 2 4]. hope to make sense

Best Answer

xy = [x;y];
minxy = min(xy);
maxxy = max(xy);
g = x - y < 0;
d = x;
d(g) = maxxy(g);
d(~g) = minxy(~g);