Averaging of "similar data" in vector

1 回表示 (過去 30 日間)
Jason
Jason 2017 年 2 月 21 日
編集済み: Jan 2017 年 2 月 21 日
Hi. I have a vector x as below. There are clearly "groups" of data. I want to find the average of each group
This was my attempt, but I've come to a halt: Not sure what to do next.
D=unique(double(x))
F=diff(D)
F1=F;
F1(F1<20)=[]
idx=find(F1==F)
x =
72×1 uint16 column vector
13
13
13
13
14
14
14
14
14
58
58
58
59
59
59
59
59
60
103
103
104
104
104
104
104
104
104
193
193
194
194
194
194
194
195
195
238
238
238
239
239
239
239
239
239
283
283
284
284
284
284
284
284
284
328
329
329
329
329
329
329
329
330
373
374
374
374
374
374
375
375
375

採用された回答

Jan
Jan 2017 年 2 月 21 日
編集済み: Jan 2017 年 2 月 21 日
If the blocks are identified by having a maximum distance thresh from the lowest to the highest element:
% UNTESTED
n = length(x);
thresh = 20
group = zeros(size(x));
lim = x(1) + thresh;
index = 1;
for k = 1:n
if x(k) > lim
index = index + 1;
lim = x(k) + thresh;
end
group(k) = index;
end
Y = splitapply(@mean, x, group); % Needs Matlab >= 2015b

その他の回答 (1 件)

Jason
Jason 2017 年 2 月 21 日
array = x';
sortedArray = sort(array);
nPerGroup = diff(find([1 (diff(sortedArray) > threshold) 1]));
groupArray = mat2cell(sortedArray,1,nPerGroup)
l=numel(groupArray)
m=zeros(l,1)
for i=1:l
Gm=groupArray{:,i}
m(i,1)=round(mean(Gm(:)))
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by