pick only the first element of elements within range

I would really appreciate some help on this. I have data like these (in the form of column vectors):
152.500000000000
166.500000000000
169
172.500000000000
174
179
184.500000000000
188
189.500000000000
191.500000000000
193
199.500000000000
203
and I want to start from the first one, find if there are following values that are within a range of 10, if there are keep the value of the checked element and delete the rest (within the range) and continue working downwards like that; if there are not following values within the range of 10 keep the value of the checked element. For instance in this dataset as an output I want to have 152.5 (no other values within range 10), 166.5 (delete 169, 172.5, 174), 179 (delete 184.5, 188), 189.5 (delete 191.5, 193, 199.5), 203. Thank you in advance

 Accepted Answer

list=[152.500000000000;166.500000000000;169;172.500000000000;174;179;184.500000000000;...
188;189.500000000000;191.500000000000;193;199.500000000000;203];
counter=1;
while counter<length(list)
indices_to_be_removed=list>=(list(counter)-10) & list<=(list(counter)+10);
indices_to_be_removed(counter)=false;%keep current value
list(indices_to_be_removed)=[];
counter=counter+1;
end

More Answers (1)

Here is one way:
in = [152.5;
166.5;
169;
172.5;
174;
179;
184.5;
188;
189.5;
191.5;
193;
199.5;
203];
n = 1;
out = in;
while n < numel(out)
if out(n+1) <= out(n) + 10
out(n+1) = [];
else
n = n+1;
end
end

Categories

Tags

Asked:

on 29 May 2017

Commented:

on 29 May 2017

Community Treasure Hunt

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

Start Hunting!