Binned data question - average and median

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)

 Risposta accettata

Andrei Bobrov
Andrei Bobrov il 3 Giu 2016
Modificato: Andrei Bobrov il 3 Giu 2016
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))]}));

4 Commenti

Jason
Jason il 3 Giu 2016
Modificato: Jason il 3 Giu 2016
Wow, thanks. As I wanted the mean of the x values withint he bin against the median of the y values, shouldn't the output be
out = [edges(1:end-1) cell2mat(accumarray(ii(t),...
(1:numel(Y1))',[],@(x){[mean(X(x)), median(Y1(x))]}))];
I also get an error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
corrected
>> X = -diff(z(:,[1,3]),1,2);
>> Y = z(:,2).^2;
>> edges = [100 105 110 115 120 125 130 135 140]';
>> [~,ii] = histc(X,edges);%[~,~,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))]}))
out =
101.9 0.00083521
106.9 0.00087616
111.9 0.00098596
116.9 0.00096721
121.9 0.00093636
126.9 0.00085849
131.9 0.00085849
134.9 0.00080089
>>
Thanks for this. Can I asked why you used histc when its recommended not to use it?
"Histogram bin counts (not recommended; use histcounts)"
Andrei Bobrov
Andrei Bobrov il 3 Giu 2016
Modificato: Andrei Bobrov il 3 Giu 2016
Now to me Matlab is not available and I used Octave

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by