필터 지우기
필터 지우기

Sort matrix by datenum

조회 수: 1 (최근 30일)
David du Preez
David du Preez 2017년 5월 9일
답변: Peter Perkins 2017년 5월 9일
I have a 88416 x 13 matrix. Column 1 contains the datenum values. I want to sort the matrix by column 1 keeping the corresponding values. It must be sorted from oldest to newest and keep the corresponding values

채택된 답변

Stephen23
Stephen23 2017년 5월 9일
편집: Stephen23 2017년 5월 9일
Where M is your matrix, use sortrows and its second optional argument to select the column:
sortrows(M,1)

추가 답변 (1개)

Peter Perkins
Peter Perkins 2017년 5월 9일
If using MATLAB R2016b or newer, you might consider using a timetable. There are lots of benefits beyond simple sorting by time, but here's how you'd do that:
>> tt = timetable(datetime(2017,5,[1;3;2;5;4]),randn(5,1),randn(5,1),randn(5,1))
tt =
5×3 timetable
Time Var1 Var2 Var3
___________ ________ ________ _________
01-May-2017 -0.1469 -0.60006 0.96697
03-May-2017 -0.85955 -0.78089 -0.075429
02-May-2017 0.69933 -0.74781 -0.58065
05-May-2017 0.94991 0.62544 1.3161
04-May-2017 0.41305 -1.3237 1.2532
>> sortrows(tt)
ans =
5×3 timetable
Time Var1 Var2 Var3
___________ ________ ________ _________
01-May-2017 -0.1469 -0.60006 0.96697
02-May-2017 0.69933 -0.74781 -0.58065
03-May-2017 -0.85955 -0.78089 -0.075429
04-May-2017 0.41305 -1.3237 1.2532
05-May-2017 0.94991 0.62544 1.3161
Prior to R2016b, you could also use a table that contains a datetime variable. sortrows would also work on that.

카테고리

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