How to pick data from one set with a condition from another?

I have two sets of data. One represents a sensor measuring CO2, humidity and temperature, while the other logs the opening and closing of the window (in terms of 0 and 1). Both sets of data have a time stamp written by the logger. I have to write a short code that will basically tell me what was the data of the sensor right before the window was open. What I know: I know that I have to create an index which will give me all times when the window was open.
What I don't know #1: How will I create this index, which needs to return me the time when the first 1 appeared, so if I have:
TIME_1 0
TIME_2 1
TIME_3 1
TIME_4 1
TIME_5 0
TIME_6 0
TIME_7 1
TIME_8 1
I only want time TIME_2 and TIME_7.
What I don't know #2: When I manage to extract those time values, how am I going to pick the closest (but smaller) value for the whole array?
I hope my question is understandable, I really appreciate your help!

 Risposta accettata

dpb
dpb il 28 Mag 2016
Modificato: dpb il 28 Mag 2016
1. Assume the window position is in an array posn; use whatever is your variable, obviously.
iOpen=[false diff(posn)==1]; % logical vector of positions
idxOp=find([false diff(posn)==1]); % alternatively, corresponding positions
2. OK, w/ the additional explanation, the point prior to the change to "open" is simply the above positions less one. In the first case of the logical vector, simply put the leading false value at the end instead of the beginning so the vector has the full length, for the index values, it's the above minus one.
iOpen=[diff(posn)==1 false]; % logical vector of positions
idxOp=find([diff(posn)==1]); % alternatively, corresponding positions
Example using your data above--
>> posn % your window time,position array
posn =
1 0
2 1
3 1
4 1
5 0
6 0
7 1
8 1
>> diff(T(:,2)) % Note what diff(position_column) returns--
ans =
1
0
0
-1
0
1
0
>> idxOp=find(diff(T(:,2))==1); % so the last position before open
>> T(idxOp,:) % use to address the array
ans =
1 0
6 0
>>
You'll simply use the index vector in the other array(s) for the corresponding properties in the same fashion (assuming, of course, that there are the same number of entries at the same time in each data set of position and recorded data).

3 Commenti

Sorry, my vocabulary in this field can be misleading since I don't have much experience. I will be a little more specific.
So, my window sensor gives a .csv output with textdata (time) and number (0 and 1 which indicate the window position).
My other sensor gives as well a .csv output with one text column (time), and 3 data columns (co2, humidity and temperature).
What I need to do is to check what were the values of co2, temp and humidity prior to the window opening. Basically I want to know the reason the window was opened. Since I have very little programming knowledge, I thought the right logic could be to extract the time (formatted to a number with datetonum) when we had a window opening and then pick the data from the other set according to the timestamps we got. My problem is that I don't know how to write a code which will take this time index martix and compare them to my sensor in a way that it takes the closest value to our 1st row of time index, says for example: this is row 43 and then takes the co2 and temp value from row 43. That way I have my first periodic data. Putting all that in a loop would give me periodic values right before window openings.
So that is my logic but I lack some skills to figure out which commands to use.
[dpb moved Nedim's comment from Answer to inline comment below]...
Thank you, your answer is helpful. But concerning the other part; no my 2 data sets don't have the same number of entries because the temperature sensor logged data after a change occurs (so multiple values per hour occurred) and the window was opened a few times per day. In this case I was hoping there is a function which can associate the time from the window data set and pick the value from the other data set at the similar time.
dpb
dpb il 1 Giu 2016
Modificato: dpb il 1 Giu 2016
Then convert the time stamps (which you didn't give format for so can't give specifics but see the datetime class if have recent release or look up date numbers if earlier). You've got the particular points of interest for the one file; simply do a lookup of those values in the other list for the nearest neighbor. find with the first or last option and k=1 is one pretty simple way to do this with the proper direction of a test >= or <= depending on whether you're looking for the value just after or just before the particular time.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 28 Mag 2016

Modificato:

dpb
il 1 Giu 2016

Community Treasure Hunt

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

Start Hunting!

Translated by