The approaches suggested by Bob Nbob and ME each work if the only values x can take in the range [1, 4] are integer values. If it can take values like 2.5 or pi, I'd use ismember.
x = [1, 2, 2.5 pi, 4, 42]
p = x.^2 + x
M = ismember(x, 1:4)
y = ones(size(x))
y(M) = p(M)
y(~M) = x(~M)
0 Comments
Sign in to comment.