Trying to sort unique values in cell array based on different column

3 ビュー (過去 30 日間)
Marcin Brynda
Marcin Brynda 2022 年 6 月 13 日
コメント済み: Voss 2022 年 6 月 14 日
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.

採用された回答

Voss
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,:)
result = 8×2 cell array
{'Type1'} {'1'} {'Type2'} {'2'} {'Type3'} {'3'} {'Type4'} {'4'} {'Type5'} {'5'} {'Type6'} {'6'} {'Type7'} {'7'} {'Type8'} {'8'}
  2 件のコメント
Marcin Brynda
Marcin Brynda 2022 年 6 月 14 日
Yes, this is exactly, what I was looking for! Thanks a lot.
Voss
Voss 2022 年 6 月 14 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by