Select row from matrix based on month with datenum

1 view (last 30 days)
Hi. I have 2 matrices. A(12x1) contains month averages(Jan-Dec) and B(437x14) which contains daily values for specific days from 2007-2016. column 1 of matrix B contains datenum values. Is it possible to select a row from B if the value in column 9 is less than the corresponding monthly average from A.
For example: A(1,1) is for January and the average is 227.24. B(1,1) is 2/01/2006 and B(1,9) is 226.01. Then I would want to select that row and create a new matrix containing it as it is less than the corresponding monthly average.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 8 Jun 2017
Edited: Andrei Bobrov on 8 Jun 2017
Please attach file with your datas (A and B):
out = B(A(month(datetime(B(:,1),'InputFormat','yyyy-MM-dd'))) > B(:,9),:);

More Answers (1)

Peter Perkins
Peter Perkins on 9 Jun 2017
Andrei's solution also works out nicely with timetables:
>> monthlyAvg = timetable(rand(3,1),'RowTimes',datetime(2017,1:3,1,'Format','MMM-yyyy')')
monthlyAvg =
3×1 timetable
Time Var1
________ ________
Jan-2017 0.078169
Feb-2017 0.38762
Mar-2017 0.17663
>> dailyData = timetable(rand(90,1),'RowTimes',datetime(2017,1,1:90)')
dailyData =
90×1 timetable
Time Var1
___________ _________
01-Jan-2017 0.66487
02-Jan-2017 0.65827
[snip]
30-Mar-2017 0.11071
31-Mar-2017 0.64954
>> dayToMonth = dailyData.Time.Month
dayToMonth =
1
1
[snip]
3
3
>> prunedData = dailyData(dailyData.Var1 < monthlyAvg.Var1(dayToMonth),:)
prunedData =
20×1 timetable
Time Var1
___________ _________
15-Jan-2017 0.018667
02-Feb-2017 0.056749
05-Feb-2017 0.30348
06-Feb-2017 0.050802
08-Feb-2017 0.14254
12-Feb-2017 0.052114
14-Feb-2017 0.1085
16-Feb-2017 0.37547
19-Feb-2017 0.05188
21-Feb-2017 0.17325
26-Feb-2017 0.18982
28-Feb-2017 0.29788
03-Mar-2017 0.13819
05-Mar-2017 0.090778
10-Mar-2017 0.12096
14-Mar-2017 0.069508
16-Mar-2017 0.13549
21-Mar-2017 0.0411
29-Mar-2017 0.0070522
30-Mar-2017 0.11071

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!