Sort Cell Array after accumarray function

조회 수: 2 (최근 30일)
MakM
MakM 2022년 12월 26일
댓글: MakM 2022년 12월 27일
I have following code;
row_f=({0;0;0;1;2;3;4;0});
row_s=({'a';'a';'b';'b';'c';'c';'a';'b'});
t={'12/09/2022 04:28:01 PM';'12/09/2022 04:28:02 PM';'12/09/2022 03:28:03 PM';'12/09/2022 03:28:04 PM';'12/09/2022 02:28:05 PM';'12/09/2022 02:28:06 PM';'12/09/2022 04:28:03 PM';'12/09/2022 03:28:05 PM'};
all=[row_f, row_s,t];
[~,~,X] = unique(all(:,2));
dataset= accumarray(X,1:size(all,1),[],@(r){all(r,:)})
dataset = 3×1 cell array
{3×3 cell} {3×3 cell} {2×3 cell}
disp(dataset{1,1});
{[0]} {'a'} {'12/09/2022 04:28:01 PM'} {[0]} {'a'} {'12/09/2022 04:28:02 PM'} {[4]} {'a'} {'12/09/2022 04:28:03 PM'}
disp(dataset{2,1});
{[0]} {'b'} {'12/09/2022 03:28:03 PM'} {[1]} {'b'} {'12/09/2022 03:28:04 PM'} {[0]} {'b'} {'12/09/2022 03:28:05 PM'}
disp(dataset{3,1});
{[2]} {'c'} {'12/09/2022 02:28:05 PM'} {[3]} {'c'} {'12/09/2022 02:28:06 PM'}
If we see the timestamp in the third column, timestamp in the first cell (dataset{1,1}) is greater than dataset{2,1} and dataset{3,1}; How do I sort the timestamp acc to ascending order after accumarray function so I can have the new dataset cell values as dataset{3,1} in the first cell because its timestamp is smaller. then dataset{2,1} in the second row cell and finally the datatset{1,1} in the thirst row cell.
like the new value for the dataset would be:
dataset = 3x1 cell array
{2x3 cell}
{3x3 cell}
{3x3 cell}
  댓글 수: 1
Matt J
Matt J 2022년 12월 26일
편집: Matt J 2022년 12월 26일
If, as in your example, the time stamp is always the same for a given alphabetic label in column 2, it is not clear why you need both.
If they are not always the same, it is not clear how you want them sorted in that case.

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

채택된 답변

Matt J
Matt J 2022년 12월 26일
편집: Matt J 2022년 12월 26일
row_f=[0;0;0;1;2;3;4;0];
row_s=({'a';'a';'b';'b';'c';'c';'a';'b'});
t={'12/09/2022 04:28:01 PM';'12/09/2022 04:28:02 PM';'12/09/2022 03:28:03 PM';'12/09/2022 03:28:04 PM';'12/09/2022 02:28:05 PM';'12/09/2022 02:28:06 PM';'12/09/2022 04:28:03 PM';'12/09/2022 03:28:05 PM'};
T=sortrows( table(row_f(:), row_s(:),t(:)) , [3,2]);
[~,~,id]=unique(T{:,2},'stable');
dataset=splitapply(@(x){x},table2cell(T),id); dataset{:}
ans = 2×3 cell array
{[2]} {'c'} {'12/09/2022 02:28:05 PM'} {[3]} {'c'} {'12/09/2022 02:28:06 PM'}
ans = 3×3 cell array
{[0]} {'b'} {'12/09/2022 03:28:03 PM'} {[1]} {'b'} {'12/09/2022 03:28:04 PM'} {[0]} {'b'} {'12/09/2022 03:28:05 PM'}
ans = 3×3 cell array
{[0]} {'a'} {'12/09/2022 04:28:01 PM'} {[0]} {'a'} {'12/09/2022 04:28:02 PM'} {[4]} {'a'} {'12/09/2022 04:28:03 PM'}

추가 답변 (1개)

the cyclist
the cyclist 2022년 12월 26일
편집: the cyclist 2022년 12월 26일
Here is one way:
% Your code
row_f=({0;0;0;1;2;3;4;0});
row_s=({'a';'a';'b';'b';'c';'c';'a';'b'});
t={'12/09/2022 04:28:01 PM';'12/09/2022 04:28:02 PM';'12/09/2022 03:28:03 PM';'12/09/2022 03:28:04 PM';'12/09/2022 02:28:05 PM';'12/09/2022 02:28:06 PM';'12/09/2022 04:28:03 PM';'12/09/2022 03:28:05 PM'};
all=[row_f, row_s,t];
[~,~,X] = unique(all(:,2));
dataset= accumarray(X,1:size(all,1),[],@(r){all(r,:)})
dataset = 3×1 cell array
{3×3 cell} {3×3 cell} {2×3 cell}
% Sort it
[~,sortIndex] = sort(cellfun(@(x)x(1,end),dataset));
datasetSorted = dataset(sortIndex,:)
datasetSorted = 3×1 cell array
{2×3 cell} {3×3 cell} {3×3 cell}
  댓글 수: 1
MakM
MakM 2022년 12월 27일
Thanks cyclist for the answer, it worked too

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

카테고리

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