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 Exchange에서 Dates 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