anonymous, piecewise-defined function that has NaN as response for a certain subdomain
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Thomas Schäfer
il 31 Ago 2022
Commentato: Thomas Schäfer
il 1 Set 2022
I want to make an Matlab anonymous, piecewise-defined function that has NaN as response for a certain subdomain, but I am uncertain how to do this.
For an anonymous, piecewise-defined function without NaN response like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1112990/image.jpeg)
I would use a script like this:
g=@(x) (x) .* (x<=-1)+...
(x+1) .* (x>-1 & x<1)+...
(-x+4) .* (x>=1);
I would apriciate if someone would give me a tip how to include a subdomain that has NaN as response in my anonymous function, let say g(x)=NaN for 2<x<=4. I do not have access to any relevant toolboxes.
0 Commenti
Risposta accettata
Walter Roberson
il 31 Ago 2022
... + 0./~(2<x & x<=4)
If the value is not in the range then the basic logic test returns false and ~ that is true which is 1 and 0./1 is 0 so 0 would be added which would have no effect.
If the value is in the range then the basic logic test returns true and ~ returns false which is 0 and 0./0 is nan.
Più risposte (1)
Torsten
il 31 Ago 2022
g=@(x) (x) .* (x<=-1)+...
(x+1) .* (x>-1 & x<1)+...
(-x+4) .* (x>=1);
x = [-2,0,1.5,2.5,5];
gx = g(x);
gx(x>2 & x<=4)=NaN
0 Commenti
Vedere anche
Categorie
Scopri di più su Monte Carlo Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!