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

4 views (last 30 days)
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.

Accepted Answer

Voss
Voss on 13 Jun 2022
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'}

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!