I want to compare two data sets at slightly offset times.

I have two sets of data, one called "MCA" and one called "Tank". I want to find the datetime where MCA = 10 as long as Tank has been equal to 0 for at least 10 minutes before and after the time MCA = 10. In datenum format, this corresponds to a time difference of +- 0.01. Is there a simple way to do this?

9 Comments

If it makes it easier, Tank doesn't necessarily have to = 0 for ten mins before and after, it just CANNOT = 1 within that time period.
Do you have a constant sampling time? Do MCA and Tank have the same time vector? If they do and the sampling time is constant, it would be easier.
No, the sampling time is different for each, and both their vectors are different. That's why I'm so stumped.
If I look at the raw data of Tank, it is almost always 0. It can only jump to 1 a few minutes before or after MCA goes to 10. If it does jump to 1, that tells me it is Case 1 in my analysis. However, some of the times it stays at 0 this whole duration which means it is Case 2 in my analysis. I can easily tell a Case 1 by only looking to see when Tank equals 1. The hard part is finding Case 2 when Tank equals 1 and MCA equals 10 because their time stamps are offset a few minutes.
Maybe you can start by the simplest thing, get the datetime where MCA = 10:
index10 = find(abs(MCA-10) < eps); % of course you can tune eps
time10 = MCA_time(index10);
Then you can test for every value in time10 if it meets your second criteria ( 'Tank has been equal to 0 for at least 10 minutes before and after'). For each value the test could look like:
% find the index corresponding to 10 minutes before (-0.01):
[~,iBegin] = min(abs( Tank_time - (time10(indexTest)-0.01) ));
% find the index corresponding to 10 minutes after (+0.01):
[~,iEnd] = min(abs( Tank_time - (time10(indexTest)+0.01) ));
% Test if there is a 1 within this period:
notOK = any(Tank(iBegin:iEnd) == 1);
I hope it helps
EDIT: not knowing the name of your variables, I named Tank_time the time stamp associated to Tank, MCA_time the time stamp associated to MCA. indexTest is the index of the value in time10 that is being tested.
Alice, I wasn't able to get this to work, as I'm not sure what you mean by IndexTest or MCA_time.
As is usual, it would be easier if you'd provide some sample data(*) that illustrates the issue and the desired result. Doesn't take a lot; generally a sample of 10-20 records can suffice to illustrate virtually any situation such as this; just over a sample subspace instead of the full problem.
(*) Or a script that would generate some sample data would be fine, too....
Here is the an example of the issue. I get a vector of dates (in datenum format) for MCA that looks as follows.
datesMCA =
1.0e+05 *
7.3685
7.3686
7.3686
7.3686
7.3686
7.3686
7.3686
7.3686
7.3687
This array consists of all the dates when MCA = 10. There are 3 separate events here (First one is at 7.3685 and lasts very briefly, next is at 7.3686 and lasts a few mins, and last one is at 7.3687 and lasts briefly). Now, over the same time period (the month of June), I get an array of dates when Tank = 1. This is seen below. What I want to do is see for each value in datesMCA whether there is a value in datesTank close to it or not (within a few minutes).
datesTank =
7.3685e+05
Not enough precision there to tell the difference but I think I now grok the issue. I'd first suggest switching to the new datenum class instead of datenum.
The list above is only three different values at that resolution; using datestr instead would show the underlying times...
Based on the description albeit not sufficient data to try it, if you have R2015a or later, see
doc uniqeutol
which might just solve your problem in a one-liner. What the doc doesn't say (and I can't test as don't have that recent a release) is whether it will work on datetime inputs or not. Since datenum is just a double, however, it will work on it which is what you've got at the moment anyway. Set the tolerance at 10/(24*60) and 'DataScale',1 to make absolute comparison. The key question is how do we "know" there are three separate events in the data? That's where we need to have the whole set of information available to use in the solution, not just this list of times...

Sign in to comment.

Answers (0)

Categories

Asked:

on 21 Jun 2017

Edited:

on 22 Jun 2017

Community Treasure Hunt

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

Start Hunting!