MATLAB: Compare elements from two matrix.

maxim

Hi!
I want to compare elements for two matrix and then create another matrix with maximal element (comparing abs(x1) i abs(x2), not x1 i x2).
I wrote this:
But maybe it's possible to do it quicker and more efficient?
for i = 1:numel(x1)
if (abs(x1(i)) > abs(x2(i)))
x(i) = x2(i);
else
x(i) = x1(i);
end
end

Best Answer

How about this?
x = x1;
idx = abs(x1) < abs(x2);
x(idx) = x2(idx);