Sorting data from table

조회 수: 5 (최근 30일)
Jiri Soucek
Jiri Soucek 2018년 10월 15일
편집: Andrei Bobrov 2018년 10월 18일
Hi, I have a table T (8766x4) columns are - month, day, year and Flow rate (that is 24 years of data - 8766 days). I need to sort flow rate for each year separately and descend flow rate for each year and then plot.
P.S. I am afraid this approach with tables is wrong

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 10월 15일
편집: Andrei Bobrov 2018년 10월 18일
T - your table with variables: month, day, year and prutok.
ii = findgroups(T.year);
Prutok = accumarray(ii,T.prutok,[],@(x){sort(x,'descend')});
for jj = 1:numel(Prutok), plot(Prutok{jj}); hold on; end
other variant
Days1 = day(datetime(T{:,{'year','month','day'}}),'dayofyear');
c = findgroups(T.year);
Anew = sort(accumarray([Days1,c],T.prutok,[],[],nan),'descend');
plot(Anew)
(added)
and the best variant by Guillaume and Peter
hold on
varfun(@(x)plot(sort(x,'descend')),T,'GroupingVariable','year','InputVariable','prutok');
hold off
  댓글 수: 2
Guillaume
Guillaume 2018년 10월 15일
Assuming that's what is wanted, this is probably simpler:
varfun(@(col) plot(sort(col, 'descend'), T, 'GroupingVariables', 'year', 'InputVariables', 'prutok')
Jiri Soucek
Jiri Soucek 2018년 10월 16일
Andrei Bobrov Thanks a lot :)

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

추가 답변 (1개)

Guillaume
Guillaume 2018년 10월 15일
Simple:
sortedtable = sortrows(yourtable, 'year', 'flowrate', {'ascend', 'descend'}) %sort ascending by year, and for identical year, descending by flow rate
where 'year' and 'flowrate' are the variable names of your year and flow rate column respectively.
A table is the right approach.
  댓글 수: 2
Jiri Soucek
Jiri Soucek 2018년 10월 15일
:( It doesnt work (it says that variable 'year' its class is 'double' and more if it would/will work i need to plot it to get this
I allready manage to plot the values by this method:
r=1994; while r<=2017
r=r+1;
Q_rocni2=T.prutok(T.year==r);
Q_rocni2_sort=sort(Q_rocni2,'descend'); %seřazen průtok za 1 rok
plot(Q_rocni2_sort,'b'); hold on
but I would like to have values in table/matrix, where rows are flowrates and one column is one year for example. Because I nee to work with values furthemore
Thanks
Peter Perkins
Peter Perkins 2018년 10월 17일
I think something like Guillaume's is the right way to go: sort the data by time, then make a plot for each year. From the plot you made, it seems like your original description is not accurate -- it looks to me like you want to plot flow rate by time, overlaying each year.
So: sortrows using year/month/day, then use varfun to make the plots (with hold on/of before and after).

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by