how can I generate the function n without having to resort to Inline function?
1 view (last 30 days)
Show older comments
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
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().
Accepted Answer
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.
2 Comments
Star Strider
on 20 Sep 2014
My pleasure!
Meet your new best friend, Anonymous Functions! They are extremely useful in a variety of situations.
More Answers (0)
See Also
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!