Adapting a dataset such that it contains unique values

2 次查看(过去 30 天)
Hello,
I'm facing the following problem:
I have a matrix A(X,1) which contains time data. The time is given in steps of 20 seconds, so 0, 30, 75 etc. means for example 05:00:00, 05:10:00, 05:25:00 [hrs:min:sec] respectively.
Now the problem is some actions X have the same time value. This shouldn't be the case as each action should take place at a unique time.
What I want to do is to add a timestep of 20 or 40 seconds to a time (i) which is the same as the value before (i-1). Though the new value (i) shouldn't overshoot or equal the time of the next value (i+1).
An example is the following:
A(1:6,1)= 225 [06:15:00]
A(7:19,1)= 240 [06:20:00]
A(20:22)= 255 [06:25:00]
Now for the first case A(1:5,1) there are only 6 values which are the same. As a result, the timevalues of A(2:6,1) should be adapted such that they are not equal to A(1,1) and are still smaller than A(7,1). To do so, 2 time steps can be added to each value. I.e. A(2,1)= 227, A(3,1)= 229 etc.
However, for A(7:19,1) only 1 time step can be added as 2 time steps will lead to an overshoot w.r.t. A(20,1).
So if there are <8 of the same time values, 2 time steps can be added; if >=8 then only 1 time step can be added.
Can anyone help me writing a code for this procedure?

采纳的回答

Guillaume
Guillaume 2016-11-4
Possibly,
A = [repmat(225,6,1); repmat(240,13, 1); repmat(255,3, 1)]
startidentical = find(diff([-Inf; A; Inf])); %location of start of each sequence, and 1 past the end
A(end+1) = A(end) + diff(startidentical(end-1:end)) * 2; %add extra time for last sequence
for seqiter = 1:numel(startidentical)-1
seqindices = startidentical(seqiter) : startidentical(seqiter+1)-1;
A(seqindices) = A(seqindices) + (0:numel(seqindices)-1).' * floor((A(startidentical(seqiter+1)) - A(startidentical(seqiter))) / numel(seqindices));
end
A(end) = [] %remove added element

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Working with Signals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by