How can I use a for loop counter in an anonymous function?
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!