-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownsampToMatch.asv
74 lines (59 loc) · 2.01 KB
/
downsampToMatch.asv
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
function indexCell = downsampToMatch(varargin)
resolution=0;
nReps = 1;
validScalarPosNum = @(x) isnumeric(x) && isscalar(x) && (x > 0);
done = false;
charFlag = false; % check if char inputt arg has happened yet
iarg=1;
while ~done
if isnumeric(varargin{iarg}) % input data
if
X{iarg} = varargin{iarg};
elseif ischar(varargin{iarg}) % Name, Value pair
charFlag=true;
if strcmp(varargin{iarg},'resolution')
resolution = varargin{iarg+1};
iarg=iarg+1; % skip next arg
if ~validScalarPosNum(resolution)
error('''resolution'' must be a positive scalar')
end
elseif strcmp(varargin{iarg},'nReps')
nReps = varargin{iarg+1};
iarg=iarg+1; % skip next arg
if ~(isreal(nReps) && rem(nReps,1)==0 && nReps>0)
error('''nReps'' must be a positive integer')
end
else
error(' Valid (Name,Value) pairs are ''resolution'' and ''nReps''');
end
end
iarg=iarg+1;
if iarg>numel(varargin)
done=true;
end
end
if resolution==0 % if no resolution provided, use histcounts default
[~, edges] = histcounts([x,y]);
resolution=edges(2)-edges(1);
end
minVal = min(cellfun(@min, X));
maxVal = max(cellfun(@max, X));
minValRound = floor(minVal) + floor( (minVal-floor(minVal))/resolution) * resolution;
maxValRound = floor(maxVal) + ceil( (maxVal-floor(maxVal))/resolution) * resolution;
for ii = 1:numel(X)
Xcount(:,ii) = histcounts(X{ii},minValRound:resolution:maxValRound);
Xdisc(:,ii) = discretize(X{ii},minValRound:resolution:maxValRound);
end
minCounts = min(Xcount,[],2);
indexCell = cell(numel(X),nReps);
for irep=1:nReps
for ival = 1:numel(minCounts)
nSamps = minCounts(ival);
for ii=1:numel(X)
allVals = find(Xdisc(:,ii)==ival);
samp_idx = randsample(numel(allVals),nSamps);
indexCell{ii,irep} = cat(1,indexCell{ii,irep},allVals(samp_idx));
end
end
end
end