MATLAB: How can I generate the function n without having to resort to Inline function

vector inline timeshift time scale time transformation

x=[-2:0.01:2];
p = [(x>=0)-(x>=1)];
r = p.*x;
n = r(x)+r(-x+2);
the last line is giving me an error, how can I achieve last line without having to use inline function and using vector method?

Best Answer

You are correct, inline functions are being abandoned in favour of ‘anonymous functions’.
Using anonymous functions here, I believe I understand what you want:
x=[-2:0.01:2];
p = @(x) [(x>=0)-(x>=1)];
r = @(x) p(x).*x;
n = r(x)+r(-x+2);
figure(1)
plot(x,n)
grid
xlabel('x')
ylabel('n')
I added the plot simply because it makes it easier to see the output.