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 pathcmap.m
More file actions
193 lines (167 loc) · 5.43 KB
/
cmap.m
File metadata and controls
193 lines (167 loc) · 5.43 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function [B] = cmap(A, fun, varargin)
% CMAP Apply a function to all elements of a collection, and return the results.
%
% SYNTAX:
% B = cmap(A, @fun)
% B = cmap(A, @fun, dim)
%
% INPUT:
% A = an N-D array (vector, matrix, or multidimensional array); a cell
% array (of any number of dimensions); or a struct array (of any
% number of dimensions).
% 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.
%
% OUTPUT:
% B = values from A, after processing by 'fun'.
% Note that 'cmap' tries to intelligently adapt the type and shape of
% 'B' to the type of output produced by 'fun'.
%
% FLAG ARGUMENTS:
% useParallel = if given, uses a 'parfor' loop, for parallel processing.
%
% SEE ALSO:
% arrayfun, cellfun
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Process & validate input
ndArrayProcessing = false;
if ~isempty(varargin)
if isnumeric(varargin{1}) && isscalar(varargin{1})
% Second syntax, for N-D numeric arrays, specifying 'dim' to iterate
% over.
if ~isnumeric(A)
error('CMAP:InvalidArgument', 'Invalid collection: numeric N-D array expected.');
end
origSize = size(A);
dim = varargin{1};
[A, elemSize, dimPermute] = splitByDim(A, dim);
varargin(1) = []; % pop
ndArrayProcessing = true;
end
end
if ~isa(fun, 'function_handle')
error('CMAP:InvalidMapFunction', 'Handle to map function expected.');
end
% Figure out what syntax the mapping function is using.
funType = nargin(fun);
if funType < 1 || funType > 2
error('CMAP:InvalidMapFunction', 'Invalid map function: invalid number of arguments.');
end
% Key-value pair, and flag arguments
defArgs = struct(...
'ignoreErrors', false ...
, 'useParallel', false ...
);
args = pargs(varargin, defArgs, {'ignoreErrors','useParallel'});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Perform mapping
B = cell(size(A));
ignoreErrors = args.ignoreErrors;
if args.useParallel
parfor i = 1:numel(A)
item = A(i);
if iscell(item) && isscalar(item)
item = item{1};
end
try
switch funType
case 1
B{i} = fun(item);
case 2
B{i} = fun(item, i);
end
catch err
if ignoreErrors
B{i} = [];
else
rethrow(err);
end
end
end
else
for i = 1:numel(A)
item = A(i);
if iscell(item) && isscalar(item)
item = item{1};
end
try
switch funType
case 1
B{i} = fun(item);
case 2
B{i} = fun(item, i);
end
catch err
if ignoreErrors
B{i} = [];
else
rethrow(err);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Process output
outputIsScalar = false(size(A));
for i = 1:numel(B)
outputIsScalar(i) = isscalar(B{i}) && ~isobject(B{i}) && ~isa(B{i}, 'function_handle');
end
if isempty(B)
B = [];
elseif ndArrayProcessing
if all(outputIsScalar(:))
% Always output a column vector in this case.
B = reshape(cell2mat(B), [length(B) 1]);
elseif isequal(size(B{1}), elemSize)
% Reverse splitting.
B = unsplitByDim(B, dimPermute, origSize);
end
elseif all(outputIsScalar(:))
B = cell2mat(B);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [splitA, elemSize, dimPermute] = splitByDim(A, dim)
if dim == 0
splitA = A(:);
else
if dim > ndims(A)
error('CMAP:InvalidDimension', 'Invalid dimension %d: input only has %d dimensions.', dim, ndims(A));
end
% Permute the dimensions of the N-D matrix so that the dimension of
% interest is the last one.
dimPermute = [(1:(dim-1)) ((dim+1):ndims(A)) dim];
A = permute(A, dimPermute);
% Now split into sub-matrices on the given dimension.
subShape = size(A);
nSplit = subShape(end);
subShape = subShape(1:end-1);
if isscalar(subShape)
subShape = [subShape 1];
end
subShapeNumEl = prod(subShape);
splitA = cell(nSplit,1);
for j = 1:nSplit
startIdx = (j-1) * subShapeNumEl + 1;
endIdx = j * subShapeNumEl;
splitA{j} = reshape(A(startIdx:endIdx), subShape);
end
elemSize = subShape;
end
end
function [unsplitB] = unsplitByDim(B, dimPermute, origSize)
unsplitB = permute(zeros(origSize), dimPermute);
subShape = size(B{1});
subShapeNumEl = prod(subShape);
nSplit = length(B);
for j = 1:nSplit
startIdx = (j-1) * subShapeNumEl + 1;
endIdx = j * subShapeNumEl;
unsplitB(startIdx:endIdx) = B{j}(:);
end
unsplitB = ipermute(unsplitB, dimPermute);
end
end