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

 採用された回答

Star Strider
Star Strider 2016 年 8 月 14 日

0 投票

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 件のコメント

Elric
Elric 2016 年 8 月 14 日
Hi Start Strider,
Thank you once again. That was indeed very clever.
Elric
Star Strider
Star Strider 2016 年 8 月 14 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2016 年 8 月 14 日

コメント済み:

2016 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by