Averaging of "similar data" in vector

1 次查看(过去 30 天)
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 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