To apply the passed function along the second dimension:
Aggregate = @(x,fun) fun(x,2);
As you see, this calls the passed function (fun
) on the input (x
) , with a fixed extra argument 2
to indicate the dimension along which the function will operate. This will work for any function that, like sum
, accepts the dimension as a second argument.
Examples:
>> Aggregate([1 2; 3 4], @sum)
ans =
3
7
>> Aggregate([1 2; 3 4], @prod)
ans =
2
12
To apply the passed function along a specified dimension:
Aggregate = @(x,fun,dim) fun(x,dim);
Example:
>> Aggregate([1 2; 3 4], @sum, 2)
ans =
3
7