how can I generate the function n without having to resort to Inline function?

1 view (last 30 days)
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?
  3 Comments
Atique
Atique on 20 Sep 2014
Edited: Atique on 20 Sep 2014
@StarStrider I want n to be a function that has the function r + the function r with the transformation of (-x+2). I apologize for this beginner question, this is my first matlab problem.
so lets say r(x) = p(x)*x and I want n(x)=r(x)+r(-x+2)
I could easily do this problem with inline function but I am trying to do it with vector method.
Image Analyst
Image Analyst on 20 Sep 2014
Edited: Image Analyst on 20 Sep 2014
Does the 2 really mean 2, or does it mean 2 elements? In the first case, x goes from the range -4 to 0 but p and r is not defined for values in the range -4 to -2, because x doesn't go that low, so what do you want to do there? For the second case you can use conv().

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 20 Sep 2014
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.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!