Hi Everybody! I have difficulties with some logical indexing.
I have vector with time values. For example the date 1st january 2008 at 9:00:00 am would be
tvec=2008 1 1 9 0 0 and so on...
Tvec covers a whole year so it has 527040 rows and 6 columns.
How do I locate a specic period. For example 1st january 2008 from 9 to 10 am?
I tried this:
clear;clc;
period = [2008 1 1 9];
idx(:,1)=tvec(:,1)==period(1);
idx(:,2)=tvec(:,2)==period(2);
idx(:,3)=tvec(:,3)==period(3);
idx(:,4)=tvec(:,4)==period(4);
L=logical(idx);
tvec_a=tvec(L);
Thanks for any help you might have...

 Réponse acceptée

Star Strider
Star Strider le 21 Nov 2014

0 votes

I’m not sure logical indexing is necessary. The find function will probably be preferable:
dn = datenum([2008 01 01 0 0 0]); % Create Data
dnv = dn:(1/24):(dn+2); % Create Data
dv = datevec(dnv);
time_start = datenum([2008 01 01 09 00 00]);
time_end = datenum([2008 01 01 10 00 00]);
rngidx = find( (dnv>=time_start) & (dnv<=time_end));
out = dv(rngidx,:)

9 commentaires

Sean de Wolski
Sean de Wolski le 21 Nov 2014
why?
Star Strider
Star Strider le 21 Nov 2014
It just seems more straightforward in this instance, looking for a range of values.
Jakob Hannibal
Jakob Hannibal le 22 Nov 2014
Modifié(e) : per isakson le 24 Nov 2014
Okay, I think it works like this:
dnv=datenum([tvec(:,1) tvec(:,2) tvec(:,3) tvec(:,4) tvec(:,5) tvec(:,6)]);
dv = datenum(tvec);
time_start = datenum([2008 1 1 9 0 0]);
time_end = datenum([2008 1 1 10 0 0]);
rngidx = find( (dnv>=time_start) & (dnv<time_end));
out = tvec(rngidx,:)
Will test it on my data... :-)
Jakob Hannibal
Jakob Hannibal le 22 Nov 2014
If any of you are interested, I found the function "accumarray" which I think I will use. It looks like it does exactly what I need...
Star Strider
Star Strider le 22 Nov 2014
I didn’t consider accumarray for your problem because I didn’t see how it would apply. If it does what you want, post the relevant parts of your code. I’d like to see what you did.
Jakob Hannibal
Jakob Hannibal le 27 Nov 2014
Modifié(e) : Jakob Hannibal le 27 Nov 2014
I guess it only worked to some extent. It can find the measurements for each hour/day/month, but I didn't get it to separate the different days or months. So it gives me 24 measurements if I chose hour, 31 if I choose days and so on. What I really want is 24 measurements for each day, 31 measurements for each month etc... I am still think of how to do that...
To elaborate: My data is a matrix with a time in column 1:6 and measurements in col 7:10. I want to aggregate the measurements hourly, dayly, monthly and yearly. One row in the vector could be:
[2008 1 18 22 10 0 134 567 83 1] which would correspond to the 18’th of january 2008, and the last four columns is measurements…
Star Strider
Star Strider le 27 Nov 2014
I’m not certain I understand about aggregating data daily, monthly, and yearly. Do you want them, for instance, for a particular month in a particular year (or each year), or in a particular month across all years, or something else? (The same question goes for days.)
If you want them aggregated by day, that would be one matrix, by month another matrix, by year another matrix. I don’t see any other way to do it.
Selecting the appropriate column or columns in your 6-element date vector would be my approach. (You could use date numbers to do the searching, but that might be more difficult than using specific columns in your date vectors.) Either the find function or logical indexing could work, depending on what you prefer. The find function might be easier, since you’re searching for specific rows, but logical indexing could work as well, depending on what you want to do.
Jakob Hannibal
Jakob Hannibal le 27 Nov 2014
It is for an project at my university. Period is the choice of the user on how to aggregate the measurements. The description says:
If period has the value 'hour'/'day'/'month', then all measurements recorded within the same hour/day/month are combined (summed), M is equal to the number of distinct hours/days/months during which measurements were recorded, and the rows of tvec_a identify the beginning of the M time period
The output of the function is an Mx6 matrix with the aggregated Measurements and an Mx4 matrix with the aggregated Measurements...
Star Strider
Star Strider le 27 Nov 2014
I’m lost. I still don’t know if you’re supposed to aggregate particular months across all years, particular months in each given year, or what. Using find first on the years and then on the months (or days or whatever you decide) will work. My problem is that I don’t know in detail what you want to do for your project.

Connectez-vous pour commenter.

Plus de réponses (1)

per isakson
per isakson le 21 Nov 2014
Modifié(e) : per isakson le 21 Nov 2014

1 vote

Replace
L=logical(idx);
by
L=logical( all(idx,2));
idx is already logical, no need apply the function, logical.
&nbsp
The datevec-format is not appropriate for this task when it comes to longer periods. Convert to serial date number.
sdn = datenum( tvec );
sdn1 = datenum( [2008,1,1, 9,0,0] );
sdn2 = datenum( [2008,1,1,10,0,0] );
Here is a problem with numerical precision. I prefer to carefully convert to seconds (whole numbers) to make comparisons simpler.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by