How to count count number of times change occured
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have below time series data:
Time(sec) V1 V2
1 0 1
2 0 1
3 1 1
4 1 1
5 1 1
6 2 0
7 2 0
8 0 0
9 0 1
10 0 1
11 0 0
12 0 0
13 0 0
14 0 1
15 0 1
16 0 1
17 0 1
18 0 1
19 0 0
20 0 0
Chage count:
when a variable current row value is different from previous row value, then count as 1 for a particular variable, and delta in the corresponding row. For example:
For variale1(V1):Compare the data in row3(in V1 column) is different from row2, I count it as 1 in row1 under V1 column and take the delta of (row3 &row2 values for V1 column, 1-0=1), same as row 6 value is different from row5,then count it as 2(under change count), and delta is (2-1)=1, so on~
Desired Output:
Time V1(change count) V1 (Delta) V2(change Count) V2 (Delta)
1
2
3 1 1
4
5
6 2 1 1 -1
7
8 3 -2
9 2 1
10
11 3 -1
12
13
14 4 1
15
16
17
18
19 5 -1
20
Many thanks in advance~
0 个评论
回答(1 个)
Adam
2017-11-23
编辑:Adam
2017-11-23
v1 = [0 0 1 1 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0]';
v1Delta = [0; diff( v1 )];
v1ChangeCount = v1Delta;
v1ChangeCount( v1Delta ~= 0 ) = 1:nnz( v1Delta );
I don't know what kind of structure your data is in, but that at least calculates the answer for e.g. v1 with 0s where there is no other number since you can't have empty space in a vector. You can replace them with Nans if you prefer.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!