フィルターのクリア

How to group data points

4 ビュー (過去 30 日間)
Max Bernstein
Max Bernstein 2015 年 8 月 7 日
コメント済み: Max Bernstein 2015 年 8 月 7 日
Hello,
I have a set of data that I would like to perform analysis on, and it's something like:
[150 151.1 149.6 150.2 146.1 145.6 144.1 144.9 145.2 139.9 140.6 138.9 135.4 135.1 134.9 135.9 135.2 129.9 129.89 130.6]
I would like to group the data to 150, 145, 140, etc... I have tried to manipulate it using rounding/ceiling, etc.. but I couldnt figure out how to group it correctly because it varies too much, and another problem is that the number of data points arent always the same. Once I can group it, I would like to find the average of the data for each set. Is there a way to do this?
Thanks for the help!
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 7 日
Can you post the expected result for this case?
Max Bernstein
Max Bernstein 2015 年 8 月 7 日
sure, it would be something like the following, with the second column being the average.
[150 150.2250
145 145.1800
140 139.8000
135 135.3000
130 130.3900]

サインインしてコメントする。

採用された回答

Matt J
Matt J 2015 年 8 月 7 日
X=floor(X/5)*5
  1 件のコメント
Max Bernstein
Max Bernstein 2015 年 8 月 7 日
Thank you Matt, this was an elegant/simple solution that I would've never thought off!

サインインしてコメントする。

その他の回答 (3 件)

Matt J
Matt J 2015 年 8 月 7 日
編集済み: Matt J 2015 年 8 月 7 日
Or this,
edges=120:5:170;
[~,~,bin] = histcounts(X,edges);
X=edges(bin)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 7 日
編集済み: Azzi Abdelmalek 2015 年 8 月 7 日
A=[150 151.1 149.6 150.2, 146.1 145.6 144.1 144.9 145.2, 139.9 140.6 138.9]
[a,b]=histc(A,[135:5:150 155])
out=accumarray(b',(1:numel(b))',[],@(x) {A(x)})
celldisp(out)
To get the mean of each group
outmean=accumarray(b',(1:numel(b))',[],@(x) {mean(A(x))})

Prabakar kg
Prabakar kg 2015 年 8 月 7 日
arr = [150 151.1 149.6 150.2 146.1 145.6 144.1 144.9 145.2 139.9 140.6 138.9 135.4 135.1 134.9 135.9 135.2 129.9 129.89 130.6];
HISTCOUNTS is used to group the data
[cnt, edges, bin] = histcounts(arr, 125:5:155);
ACCUMARRAY to calculate SUM on the grouped data
sum = accumarray(bin', arr');
Calculate the AVERAGE. Note that this could be done in the previous step. I separated these to improve readability
avg = sum./cnt';
Hope this helps.
  1 件のコメント
Prabakar kg
Prabakar kg 2015 年 8 月 7 日
Below was the result
avg =
129.8950
132.7500
136.7333
143.2000
146.6250
150.4333

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by