Select row from matrix based on month with datenum

조회 수: 2 (최근 30일)
David du Preez
David du Preez 2017년 6월 8일
답변: Peter Perkins 2017년 6월 9일
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.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 6월 8일
편집: Andrei Bobrov 2017년 6월 8일
Please attach file with your datas (A and B):
out = B(A(month(datetime(B(:,1),'InputFormat','yyyy-MM-dd'))) > B(:,9),:);

추가 답변 (1개)

Peter Perkins
Peter Perkins 2017년 6월 9일
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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by