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

5 vues (au cours des 30 derniers jours)
Marcin Brynda
Marcin Brynda le 13 Juin 2022
Commenté : Voss le 14 Juin 2022
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.

Réponse acceptée

Voss
Voss le 13 Juin 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'}
  2 commentaires
Marcin Brynda
Marcin Brynda le 14 Juin 2022
Yes, this is exactly, what I was looking for! Thanks a lot.
Voss
Voss le 14 Juin 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by