cvmgt.m 587 Bytes
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
function z = cvmgt(x, y, p)
%CVMGT  Conditional merge of two vectors
%
%   z = CVMGT(x, y, p) return a vector z whose elements are x if p is true
%   and y otherwise.  p, x, and y should be the same shape except that x
%   and y may be scalars.  CVMGT stands for conditional vector merge true
%   (an intrinsic function for the Cray fortran compiler).  It implements
%   the C++ statement
%
%     z = p ? x : y;

  z = zeros(size(p));
  if isscalar(x)
    z(p) = x;
  else
    z(p) = x(p);
  end
  if isscalar(y)
    z(~p) = y;
  else
    z(~p) = y(~p);
  end
end