How to cluster 1D array so that approximately equal values are replaced by their average?

조회 수: 3 (최근 30일)
I have a 1D array that features "double-counts". I would like to consolidate or cluster the dataset in a way that replaces the double-counts with the average of the two double-counted values. Here's a sample dataset
xx=[185.0712 185.0715 185.5117 186.1008 186.5077 187.1303 187.5037 188.1596 188.1597 188.4993 189.1890 189.4949 190.2185 190.4903 191.2477 191.2480].'
Here's a screenshot where I've circled the relevant entries that are double counts:
Does anyone know how to do this?

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 9일
[~, ~, G] = uniquetol(xx);
xxmean = grpstats(xx, G, 'mean');
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 4월 9일
Statistics and Machine Learning Toolbox.
You can instead use
xx=[185.0712 185.0715 185.5117 186.1008 186.5077 187.1303 187.5037 188.1596 188.1597 188.4993 189.1890 189.4949 190.2185 190.4903 191.2477 191.2480].'
xx = 16×1
185.0712 185.0715 185.5117 186.1008 186.5077 187.1303 187.5037 188.1596 188.1597 188.4993
[~, ~, G] = uniquetol(xx, 1e-4);
xxmean = accumarray(G, xx, [], @mean)
xxmean = 13×1
185.0713 185.5117 186.1008 186.5077 187.1303 187.5037 188.1596 188.4993 189.1890 189.4949
Travis Briles
Travis Briles 2021년 4월 9일
I downloaded the toolbox and now both solutions work. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by