Effacer les filtres
Effacer les filtres

anonymous function + IF + fitnlm = possible?

3 vues (au cours des 30 derniers jours)
wesleynotwise
wesleynotwise le 5 Juin 2017
Commenté : Star Strider le 5 Juin 2017
Hello. Is it possible to have an anonymous function combined with an If statement in a non-regression function? something like this --->
tbl = table(x1, x2, x3, y);
if table.x1 < 30
modelfun1 = @(b,x)(...Equation (1)...);
b0 = [1 1 1 ...];
else % table.x1 > 30
modelfun2 = @(b,x)(...Equation (2)...);
b0 = [1 1 1...];
mdl1 = fitnlm(tbl, modelfun1, b0)
mdl2 = fitnlm(tbl, modelfun2, b0)
end

Réponse acceptée

Star Strider
Star Strider le 5 Juin 2017
Not an if statement. However you can use a form of ‘logical indexing’ to do essentially the same thing.
Example
x = linspace(0, 60);
b = [3 11];
modelfun = @(b,x) (x<=30).*(b(1).*x/10 - b(2)) + (x>30).*(b(1).*sin(x.*b(2)));
figure(1)
plot(x, modelfun(b,x))
grid
Logical vectors evaluate as (0,1) numeric values in calculations.
  4 commentaires
wesleynotwise
wesleynotwise le 5 Juin 2017
Noted. Cheers.
Star Strider
Star Strider le 5 Juin 2017
Cheers.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by