forked from anne-urai/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivideintobins3.m
56 lines (43 loc) · 1.45 KB
/
divideintobins3.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
46
47
48
49
50
51
52
53
54
55
56
function [binnedx, binnedy, binnedz, stdx, stdy, stdz] = divideintobins3(x, y, z, nbins)
% take two vectors and divide x them into nbins, then compute the mean
% response of y for each x
if ~exist('corrtype', 'var'); corrtype = 'Spearman'; end
%assert(~any(isnan(x)), 'x contains nans');
%assert(~any(isnan(y)), 'y contains nans');
if nbins == 2,
% use quantile rather than histcounts, we want each bin to contain the same nr of points!
qs = quantile(x, 0.5);
elseif nbins == 3,
qs = quantile(x, [0.33 0.67]);
else
qs = quantile(x, nbins - 1);
end
binnedx = nan(1, nbins);
binnedy = nan(1, nbins);
binnedz = nan(1, nbins);
stdx = nan(1, nbins);
stdy = nan(1, nbins);
stdz = nan(1, nbins);
for q = 1:length(qs) + 1,
% determine which trials belong to this quantile
if q == 1,
findtrls = find(x <= qs(q));
elseif q == length(qs) + 1,
findtrls = find(x > qs(q-1));
else
findtrls = find(x <= qs(q) & x > qs(q-1));
end
% nicer: get bin centres
% [~, idx] = min(abs(centres-x));
% assert(~isempty(findtrls), 'no trials found in this bin');
% find the mean x and y
binnedx(q) = nanmean(x(findtrls));
binnedy(q) = nanmean(y(findtrls));
binnedz(q) = nanmean(z(findtrls));
assert(~isnan(binnedx(q)));
% also compute variance
stdx(q) = nanstd(x(findtrls));
stdy(q) = nanstd(y(findtrls));
stdz(q) = nanstd(z(findtrls));
end
end