forked from sakoay/MovieSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebin.m
36 lines (31 loc) · 1.4 KB
/
rebin.m
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
function rebinned = rebin(data, grouping, dimensions, aggregateFcn, varargin)
if nargin < 3 || isempty(dimensions)
[~,dimensions] = max(size(data));
end
if nargin < 4 || isempty(aggregateFcn)
aggregateFcn = @mean;
end
for dim = dimensions
original = size(data);
standardized = [ prod(original(1:dim-1)) ...
, original(dim) ...
, prod(original(dim+1:end)) ...
];
data = reshape(data, standardized);
numGroups = ceil(original(dim) / grouping);
if islogical(data)
rebinned = false(standardized(1), numGroups, standardized(end));
elseif isinteger(data)
rebinned = zeros(standardized(1), numGroups, standardized(end), 'like', data);
else
rebinned = nan(standardized(1), numGroups, standardized(end), 'like', data);
end
iSource = 1;
for iGroup = 1:numGroups
rebinned(:,iGroup,:) = aggregateFcn(data(:,iSource:min(iSource+grouping-1, end),:), 2, varargin{:});
iSource = iSource + grouping;
end
rebinned = reshape(rebinned, [original(1:dim-1), numGroups, original(dim+1:end)]);
data = rebinned;
end
end