Free preview.You're sampling one lesson — enroll free to unlock all 10 lessons and track your progress.
Enroll free lesson
Closures and Higher-Order Functions
Closures and Higher-Order Functions
In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in MATLAB and why it matters in real work.
Why it matters
Functions that take or return functions unlock concise, composable code.
Key ideas
- Functions as values
- map / filter / reduce
- Callbacks
- Composition
In practice
Here's how it looks in idiomatic MATLAB:
nums = 1:5;
sq = arrayfun(@(x) x^2, nums); % map over array -> [1 4 9 16 25]
C = cellfun(@upper, {'a','b'}, ...
'UniformOutput', false); % needed for non-scalar output
tot = sum(nums(arrayfun(@(x) mod(x,2)==0, nums))); % filter+reduce
MATLAB note: arrayfun/cellfun require 'UniformOutput', false whenever the per-element result isn't a scalar, otherwise MATLAB errors trying to pack results into a numeric array.
Try it yourself
Exercise: In MATLAB, use map and filter to get the squares of the even numbers.
Recap
You now understand closures and higher-order functions and can apply it in MATLAB. Mark this lesson complete and continue to the next one.
