-
Notifications
You must be signed in to change notification settings - Fork 1
/
downsample_chunk.m
45 lines (41 loc) · 1.26 KB
/
downsample_chunk.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
37
38
39
40
41
42
43
44
45
function d = downsample_chunk(x,n,dim,method)
% d = downsample_chunk(x,n,dim,method)
if(nargin<3 || isempty(dim))
dim = 1;
end
if(nargin<4 || isempty(method))
method = 'mean';
end
if(isinf(n))
reshaped_x = x;
ds = size(x);
ds(dim)=1;
else
matdim = ndims(x);
s = size(x);
if(length(s)<dim)
s(end+1:dim)=1;
matdim=dim;
end
ds = s;
ds(dim) = floor(s(dim)/n);
trancated_x = subsref(x,struct('type',{'()'},'subs',{cat(2,repmat({':'},1,dim-1),{1:(n*ds(dim))},repmat({':'},1,matdim-dim))}));
reshaped_x = reshape(trancated_x,[ds(1:(dim-1)) n ds(dim) ds((dim+1):end)]);
end
switch(method)
case 'mean'
d = reshape(mean(reshaped_x,dim),ds);
case 'sum'
d = reshape(sum(reshaped_x,dim),ds);
case 'nanmean'
d = reshape(nanmean(reshaped_x,dim),ds);
case 'median'
d = reshape(median(reshaped_x,dim),ds);
case 'min'
d = reshape(min(reshaped_x,[],dim),ds);
case 'max'
d = reshape(max(reshaped_x,[],dim),ds);
otherwise
error('unknown method');
end
end