Sort matrix by date

조회 수: 2 (최근 30일)
simen ommundsen
simen ommundsen 2019년 5월 8일
댓글: Peter Perkins 2019년 5월 14일
Hello,
I have a matrix where the first collumn is date and the rest is different parameters like voltage and power etc..
The first collumn has for some reason been sorted like this: 01-jan-2019, 01-feb-2019, 01-mar2019 etc.
Is there some way that i can sort the whole matrix so that it goes like 01-jan-2019, 02-jan-2019 etc?
i have used the sort function on the datetime values and that works, but the rest of the matrix wont follow.
thanks in advance
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2019년 5월 8일
Please attach small example your data as mat-file.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 5월 8일
[~, sortidx] = sort(TheDatetimeValues);
YourMatrix = YourMatrix(sortidx,:);
  댓글 수: 13
Walter Roberson
Walter Roberson 2019년 5월 9일
timetables happen to have the property that rowtimes are unique, so it is never necessary to refer to the second or further variables to break ties.
Peter Perkins
Peter Perkins 2019년 5월 14일
Some timetables have unique row times. But it's not a requirement. In fact, in descending order of "niceness", the row times need not be
  • regularly-spaced
  • ascending
  • sorted
  • unique
  • even defined
>> tt = timetable([1;2;3;4;5],'RowTimes',datetime(2019,5,[5;5;2;1;NaN]))
tt =
5×1 timetable
Time Var1
___________ ____
05-May-2019 1
05-May-2019 2
02-May-2019 3
01-May-2019 4
NaT 5
As a result, time subscripting works a little differently than indexing by row number:
>> tt('5-May-2019',:)
ans =
2×1 timetable
Time Var1
___________ ____
05-May-2019 1
05-May-2019 2
>> tt('6-May-2019',:)
ans =
0×1 empty timetable
The idea here is that timetables give you a way to read in your very messy data (using readtimetable, since 18b) and clean up the data all inside of MATLAB.
Row names in a table do have to be unique, though.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by