This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapply.m
More file actions
48 lines (40 loc) · 1.18 KB
/
capply.m
File metadata and controls
48 lines (40 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function capply(A, fun, varargin)
% CAPPLY Run a function on all elements of a collection.
%
% SYNTAX:
% capply(A, @fun)
%
% INPUT:
% A = a vector, either numeric, a cell array, or a struct array.
% fun = a function of one of the following prototypes:
% function [b] = fun(item)
% function [b] = fun(item, idx)
% with:
% item = an item from the collection.
% idx = index of the item within the collection.
% b = a boolean, indicating if the item should be kept.
%
% NOTE: This is really just a simple wrapper around "cmap", for map functions
% that don't return anything.
%
% SEE ALSO:
% cmap
switch nargin(fun)
case 1
wrapperFun = @capply_internal1;
case 2
wrapperFun = @capply_internal2;
otherwise
error('CAPPLY:InvalidMapFunction', 'Invalid map function: invalid number of arguments.');
end
cmap(A, wrapperFun, varargin{:});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [dummy] = capply_internal1(item)
dummy = [];
fun(item);
end
function [dummy] = capply_internal2(item, idx)
dummy = [];
fun(item, idx);
end
end