How to use unique function to get data only when the number in one row increases
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
dpb
on 10 Dec 2015
>> [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 Comments
dpb
on 11 Dec 2015
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...
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!