Averaging of "similar data" in vector

1 visualizzazione (ultimi 30 giorni)
Jason
Jason il 21 Feb 2017
Modificato: Jan il 21 Feb 2017
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

Risposta accettata

Jan
Jan il 21 Feb 2017
Modificato: Jan il 21 Feb 2017
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

Più risposte (1)

Jason
Jason il 21 Feb 2017
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

Community Treasure Hunt

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

Start Hunting!

Translated by