-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindicator.m
More file actions
53 lines (49 loc) · 2.06 KB
/
indicator.m
File metadata and controls
53 lines (49 loc) · 2.06 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
% USAGE:
% arrIndicator = indicator(mtxIndices, <vDims>)
%
% DESCRIPTION:
% Create an indicator array from a list of indices
%
% ARGUMENTS:
% mtxIndices
% A matrix, each row of which contains indices into an array
% vDims (default: minimum dimensions necessary)
% A list of dimensions for the output array
%
% RETURN:
% arrIndicator
% The reqeusted indicator array
%
% NOTE:
% When the provided list of indices is a row vector, the vector is treated
% as a matrix, i.e., as specifying a single index into a multi-dimensional
% array. When the provided list of indices is a column vector, the vector is
% treated as containing linear indices. This setup allows for consistency
% with the more general setup of providing indices as rows of a matrix.
function arrIndicator = indicator(mtxIndices, vDims)
% All indices need to be positive.
assert(all(mtxIndices(:) > 0), 'indicator: all indices must be positive');
% No output dimensions were provided, making our work easy.
if nargin < 2 || isempty(vDims)
arrIndicator = (0 < accumarray(mtxIndices, 1));
elseif iscolumn(mtxIndices)
% We're using linear indexing because the input is a column vector.
% Make sure that no index is too large.
assert(max(mtxIndices) <= prod(vDims), ...
'indicator: index out of range');
arrIndicator = false([vDims, 1]);
arrIndicator(mtxIndices) = true;
else
% All of the assertions below are basically to guarantee that the
% data is in a format that `accumarray` likes.
assert(isvector(vDims));
vDims = row(vDims);
assert(length(vDims) == size(mtxIndices, 2), ...
['indicator: the number of columns of `mtxIndices` must ' ...
'equal the number of entries in `vDims`']);
assert(all(max(mtxIndices) <= vDims), ...
['indicator: index out of range; ensure that indices lie ' ...
'within the specified dimensions'])
arrIndicator = (0 < accumarray(mtxIndices, 1, vDims));
end
end