Pointer to MATLAB function?
Show older comments
So I have a for-loop in MATLAB, where either a vector x will be put through one function, say, cos(x).^2, or a different choice, say, sin(x).^2 + 9.*x. The user will select which of those functions he wants to use before the for-loop.
My question is, I dont want the loop to check what the user selected on every iteration. Is there a way to use a pointer to a function, (user defined, or otherwise), that every iteration will use automatically?
This is inside a script by the way, not a function.
Thanks
Accepted Answer
More Answers (3)
I am not entirely sure this is what you mean but here goes:
if (first_selection)
fun1 = @(x) cos(x).^2;
end
if (second_selection)
fun1 = @(x) sin(x).^2 + 9 .* x;
end
And now your loop:
for k = 1:num_Loops
x = your_values;
your_result = fun1(x);
end
Cheers!
Ghada
on 12 Nov 2012
0 votes
Hi all;
please i need an example of the application of iteration in ananymous function,
Awaiting to you response,
Best regards,
my mail is :chahd.hayet@gmail.com
Categories
Find more on Loops and Conditional Statements 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!