Filter löschen
Filter löschen

How do you write a function that can take another function as an argument?

4 Ansichten (letzte 30 Tage)
How do you write a function that can take another function as an argument?
This is pseudocode for what I want to write:
function [outputvector] = myfunction(inputfunction, myvector, arg 2, arg3,..., argn)
for i = 1:length(myvector) outputvector (i) = inputfunction(myvector(i), arg2, arg3, ...argn) end
Thanks.

Antworten (2)

Matt J
Matt J am 1 Nov. 2012
Bearbeitet: Matt J am 1 Nov. 2012
Pass a function handle as the inputfunction argument, e.g.
myfunction(@sin, myvector, arg2, arg3,..., argn);
Same kind of thing with anonymous functions
myfunction(@(x) x+1 , myvector, arg2, arg3,..., argn);

Vishal Rane
Vishal Rane am 8 Nov. 2012
Bearbeitet: Vishal Rane am 8 Nov. 2012
If you are not used to function handles, you can simply pass the function name and feval it inside. As long as both functions are anywhere on the matlab path.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by