Logical indexing for matrix

2 visualizaciones (últimos 30 días)
Jakob Hannibal
Jakob Hannibal el 21 de Nov. de 2014
Comentada: Star Strider el 27 de Nov. de 2014
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...

Respuesta aceptada

Star Strider
Star Strider el 21 de Nov. de 2014
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 comentarios
Jakob Hannibal
Jakob Hannibal el 27 de Nov. de 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 el 27 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

per isakson
per isakson el 21 de Nov. de 2014
Editada: per isakson el 21 de Nov. de 2014
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.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by