How can I use a for loop counter in an anonymous function?
Afficher commentaires plus anciens
I currently have a loop that creates anonymous functions using values from arrays, and I need the 'p' value to be used within the loop to create the anonymous function so that values from other arrays are assigned to it.
for p = 1:5
dydxLeft(:, p) = dydx(pLeft(:, p));
dydxRight(:, p) = dydx(pRight(:, p));
dfLeft(:, p) = dydx(dydxLeft(:, p));
dfRight(:, p) = dydx(dydxRight(:, p));
sub_left_dydx{p} = @(t) dydxLeft(1, p).*t.^3 + dydxLeft(2, p).*t.^2 + dydxLeft(3, p).*t + dydxLeft(4, p);
sub_right_dydx{p} = @(t) dydxRight(1, p).*t.^3 + dydxRight(2, p).*t.^2 + dydxRight(3, p).*t + dydxRight(4, p);
sub_left_df{p} = @(t) dfLeft(1, p).*t.^2 + dfLeft(2, p).*t + dfLeft(3, p);
sub_right_df{p} = @(t) dfRight(1, p).*t.^2 + dfRight(2, p).*t + dfRight(3, p);
end
The current output for the anonymous function sub_left_dydx{1} is:
@(t) dydxLeft(1,p).*t.^3 + dydxLeft(2,p).*t.^2 + dydxLeft(3,p).*t + dydxLeft(4,p)
Whereas what I am looking for is:
@(t) dydxLeft(1,1).*t.^3 + dydxLeft(2,1).*t.^2 + dydxLeft(3,1).*t + dydxLeft(4,1)
How can I achieve this?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!