MATLAB: Binned data question – average and median

.binhistcounts

Hi I have a set of data in 3 columns.
S N D
100 0.0276 0.1012
101 0.0279 0.1012
102 0.0278 0.1017
103 0.0334 0.102
104 0.0289 0.1024
105 0.0346 0.1014
106 0.0296 0.1019
107 0.0263 0.1018
108 0.0324 0.1016
109 0.0289 0.1021
110 0.0314 0.1013
111 0.0296 0.1013
112 0.0331 0.1013
113 0.0314 0.102
114 0.0334 0.1021
115 0.0301 0.1013
116 0.0312 0.1008
117 0.0308 0.1007
118 0.0339 0.1019
119 0.0306 0.1018
120 0.0311 0.1015
121 0.033 0.1021
122 0.0303 0.1016
123 0.0306 0.1019
124 0.0312 0.1016
125 0.0289 0.1021
126 0.0293 0.102
127 0.0293 0.1017
128 0.0287 0.1019
129 0.0263 0.1012
130 0.0304 0.1013
131 0.0299 0.1012
132 0.0331 0.1015
133 0.0293 0.1015
134 0.0272 0.102
135 0.0292 0.1017
136 0.0283 0.102
I normally plot x-axis (S-D) against y-axis N^2. But i want to bin the data. So I want to bin the x-axis data (S-D) into the following bins [100 105 110 115 120 125 130 135 140].
For each bin I want to find the data that belongs to that bin and in each bin calculate the average value of all signals that fall into the BIN, as well as the median value of all noise values that belong to the BIN, and then replot using these binned data.
I have started with this but not sure where to go now.
edges = [100 105 110 115 120 125 130 135 140];
X=S-D;
Y=N^2;
N = histcounts(X,edges)

Best Answer

z = [100 0.0276 0.1012
101 0.0279 0.1012
102 0.0278 0.1017
103 0.0334 0.102
104 0.0289 0.1024
105 0.0346 0.1014
106 0.0296 0.1019
107 0.0263 0.1018
108 0.0324 0.1016
109 0.0289 0.1021
110 0.0314 0.1013
111 0.0296 0.1013
112 0.0331 0.1013
113 0.0314 0.102
114 0.0334 0.1021
115 0.0301 0.1013
116 0.0312 0.1008
117 0.0308 0.1007
118 0.0339 0.1019
119 0.0306 0.1018
120 0.0311 0.1015
121 0.033 0.1021
122 0.0303 0.1016
123 0.0306 0.1019
124 0.0312 0.1016
125 0.0289 0.1021
126 0.0293 0.102
127 0.0293 0.1017
128 0.0287 0.1019
129 0.0263 0.1012
130 0.0304 0.1013
131 0.0299 0.1012
132 0.0331 0.1015
133 0.0293 0.1015
134 0.0272 0.102
135 0.0292 0.1017
136 0.0283 0.102];
X = -diff(z(:,[1,3]),1,2)
Y = z(:,2).^2
edges = [100 105 110 115 120 125 130 135 140]';
[~,~,ii] = histcounts(X,edges);
t = ii ~= 0;
Y1 = Y(t);
out = cell2mat(accumarray(ii(t),(1:numel(Y1))',[],@(x){[mean(X(x)), median(Y1(x))]}));