Sort Cell Array after accumarray function
2 次查看(过去 30 天)
显示 更早的评论
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,:)})
disp(dataset{1,1});
disp(dataset{2,1});
disp(dataset{3,1});
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
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{:}
0 个评论
更多回答(1 个)
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,:)})
% Sort it
[~,sortIndex] = sort(cellfun(@(x)x(1,end),dataset));
datasetSorted = dataset(sortIndex,:)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!