-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathResample.m
34 lines (29 loc) · 957 Bytes
/
Resample.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
% RESAMPLE Resamples a data set. You can optionally specify a parameter n that
% will detemine how many trials are included in the sample.
function sample = Resample(data, n)
fields = fieldnames(data);
% find the biggest dimension across all fields
len = zeros(size(fields));
for i = 1:length(fields)
len(i) = length(data.(fields{i}));
end
maxLen = max(len);
% determine how many points to include in the sample, defaulting to all
if(nargin < 2)
n = maxLen;
end
% create a random ordering for resampling
order = randi(maxLen, 1, n);
% resample fields that match the max length, otherwise leave them alone
sample = data;
for i = 1:length(fields)
thisField = sample.(fields{i});
if(size(thisField,1) == maxLen)
sample.(fields{i}) = thisField(order,:);
elseif(size(thisField,2) == maxLen)
sample.(fields{i}) = thisField(:,order);
else
sample.(fields{i}) = thisField;
end
end
end