How to use unique function to get data only when the number in one row increases

조회 수: 1 (최근 30일)
Hello,
I am trying to figure out on how to extract data only when the number in one of the row increases; for example:
a = [1;1;1; 2;3;4;5;6;7;8;9;10;10;10;11;11];
b = rand(16,1);
c = [b,a]
1) i want to extract the b data only when the number in 'a' increases, which are 1, 2, 3, 4, 5, - 10 without taking the data when the number in a is the same.
2) The numbers in a will go back to 1 after it reaches 255, so i want the function to still take the data when the number goes back to 1. this means that only when the number in a are the same in consequtive order then only you remove the repetition
i appreciate your help in this, please let me know if my question is not clear.

채택된 답변

dpb
dpb 2015년 12월 10일
>> [a [true;diff(a)>0]]
ans =
1 1
1 0
1 0
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
10 0
10 0
11 1
11 0
>>
The "startover" case depends somewhat on how the series is defined; in the simplest case I think it would simply be
>> a=[a;a(1:5)]; % sample dataset w/ reset...
>> [a [true;diff(a)~=0]].'
ans =
Columns 1 through 15
1 1 1 2 3 4 5 6 7 8 9 10 10 10 11
1 0 0 1 1 1 1 1 1 1 1 1 0 0 1
Columns 16 through 21
11 1 1 1 2 3
0 1 0 0 1 1
>>
Or,
>> find([true;diff(a)~=0]).'
ans =
1 4 5 6 7 8 9 10 11 12 15 17 20 21
>>
  댓글 수: 4
Sharah
Sharah 2015년 12월 10일
thank you for your answer. i did not thought about using the derivatives though. thanks!
dpb
dpb 2015년 12월 11일
It's surprising how often diff is the answer, indeed! :) The key is the fact you're looking for that difference from successive locations here. Ergo, the uninteresting positions are then zero when compared to each other. First finding (or recognizing) the underlying pattern is the key...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by