selecting certain values from a list

I have this list below (call it A), and i wish to find the last value of a group of numbers. if you look at the numbers, they are bunched together in groups (not always consecutively), and i only want the last number in each group.
A =
8
9
106
107
109
110
244
245
325
326
329
334
335
483
484
485
486
527
528
690
691
So the new list (call it B), should look like this..
B =
9
110
245
335
486
528
691
I was thinking of making a for loop (i=1:length(A)) and doing a 'find' to see if a number exists in between that number and +20 after it (each new group of numbers never exceeds beyond 20).
is this possible? thank you in advance

 채택된 답변

Paulo Silva
Paulo Silva 2011년 6월 28일

0 개 추천

M=[ 8 9 106 107 109 110 244 245 325 326 329 334 335 483 484 485 486 527 528 690 691]
d=diff(M); %find the diference between consecutive values
[M(d>1);M(end)] %get the values with diference bigger than 1
append M(end) because the last one is always the last from a group and the diference function doesn't catch it

댓글 수: 6

Nathan Greco
Nathan Greco 2011년 6월 28일
Note the comment that says "they are bunched together in groups (not always consecutively)". Yours counts groups as consecutive, while the following is in actuality one group: 325 326 329 334 335.
Oleg Komarov
Oleg Komarov 2011년 6월 28일
Andrei's solution applies well to your example data. The implicit rule is that the group is defined by the hundreds/thousands/so on digits.
Sean de Wolski
Sean de Wolski 2011년 6월 28일
I think Paulo's code mended to be >20 as the OP said would be ideal.
Oleg Komarov
Oleg Komarov 2011년 6월 28일
What if the numbers are: 398 399 400 401. Will the 3.. and the 4.. numbers be part of the same group or to separate?
Sean de Wolski
Sean de Wolski 2011년 6월 28일
OP: "I was thinking of making a for loop (i=1:length(A)) and doing a 'find' to see if a number exists in between that number and +20 after it (each new group of numbers never exceeds beyond 20)."
so basically anywhere there's a gap of more than 20 it fails.
Michael
Michael 2011년 6월 28일
if the numbers are 398 399 400 401, then in my case they will all belong to the same group. these numbers in my case represent indices for a data set, and the gap between groups of data is usually higher than 100, and each different "cluster" of close numbers usually doesnt exceed 10, but i used 20 to be safe, with the intention of only gathering the last value of each group.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 6월 28일

0 개 추천

[a b c]=unique(floor(A/100));
out = A(b)

댓글 수: 1

Michael
Michael 2011년 6월 28일
nice thinking, this worked, but for some reason when i added the longer list i have, one of them didnt show up..

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2011년 6월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by