smart way for grouping condition

Hi all,
I have a two column matrix, A. In the first column I store the values of years, e.g. 2011, 2012, etc., and in the second column there are the values of monhts, 1, 2...,12. I want to pick all the values that belong within a specific range, lets say from 3/2014 to 9/2015. I have tried the following in order to group my conditions but... it calculates sth different:
( a(:, 1) > yearStart & a(:, 2) > monthStart ) & ( a(:, 1) > yearEnd & a(:, 2) > monthEnd )
Actually the above code takes the values between 2014-2015 and between months 3-9.
Is there a clever way to do this?
Thank you in advance,
Elric

 Respuesta aceptada

Star Strider
Star Strider el 14 de Ag. de 2016
This is one way to get the appropriate rows with the dates you want:
Dates = [reshape(repmat([2014 2015], 12, 1), [], 1), repmat([1:12]', 2, 1)]; % Create Initial Date Matrix
DateNums = datenum([Dates, ones(size(Dates,1), 1)]); % Create Date Number Vector
ToInclude = datenum([2014 03 01; 2015 09 01]);
IncludedDateRows = DateNums >= ToInclude(1) & DateNums <= ToInclude(2);
Out = Dates(IncludedDateRows,:);
The ‘IncludedDateRows’ logical vector will have the rows you want. You can use that as I used it in defining ‘Out’ to get all the other columns of your data matrix.

2 comentarios

Elric
Elric el 14 de Ag. de 2016
Hi Start Strider,
Thank you once again. That was indeed very clever.
Elric
Star Strider
Star Strider el 14 de Ag. de 2016
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 14 de Ag. de 2016

Comentada:

el 14 de Ag. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by