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

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Voss
Voss il 13 Giu 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'}

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by