Trying to sort unique values in cell array based on different column
3 ビュー (過去 30 日間)
古いコメントを表示
I'm trying to sort unique values in cell array based on one column.
I tried to use the solution suggested by Andrei Bobrov, by grouping variables, but get an error message:
Error using tabular/varfun>vertcatWithNumRowsCheck (line 488)
Unable to concatenate incompatible values returned by the function '@(x)x(:)'' when applied to groups in the variable 'test2':
Dimensions of matrices being concatenated are not consistent.
I have an array with two columns, and I would like to retain only 8 unique combinations in the order dictated by the columns containing numerical values from 1 to 8.
The code is the following:
test = {'Type1', 'Type1', 'Type2', 'Type2', 'Type2', 'Type3', 'Type4', 'Type4', 'Type4', 'Type4', 'Type4', 'Type5', 'Type5', 'Type6', 'Type6', 'Type7', 'Type8';
'1' '1' '2' '2' '2' '3' '4' '4' '4' '4' '4' '5' '5' '6' '6' '7' '8'}'
T = cell2table(test);
T.test2 = str2double(T.test2 );
mis = 1
out = varfun(@(x)x(:)',T,'GroupingVariables','test1');
I would kindly appreciate an explanation as to how it doesn't work, or suggestion for another solution.
0 件のコメント
採用された回答
Voss
2022 年 6 月 13 日
test = {'Type1', 'Type1', 'Type2', 'Type2', 'Type2', 'Type3', 'Type4', 'Type4', 'Type4', 'Type4', 'Type4', 'Type5', 'Type5', 'Type6', 'Type6', 'Type7', 'Type8';
'1' '1' '2' '2' '2' '3' '4' '4' '4' '4' '4' '5' '5' '6' '6' '7' '8'}';
"I'm trying to sort unique values in cell array based on one column.... I would like to retain only 8 unique combinations in the order dictated by the columns containing numerical values from 1 to 8"
Does this do what you want?
[~,ii] = unique(test(:,2),'stable');
result = test(ii,:)
その他の回答 (0 件)
参考
カテゴリ
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!