Effacer les filtres
Effacer les filtres

block first row of a cell

1 vue (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 26 Juin 2023
Commenté : Stephen23 le 26 Juin 2023
Hi. I would like to sort the columns alphabetically or in ascending number order while keeping the rows unchanged.
Example:
I have this code:
value = {'Value';'8126';'5354';'1406';'4265'};
name = {'Name';'B';'A';'N';'R'};
union = [name,value];
I would like to sort the numbers present in 'value' resulting in this:
value_1 = {'Value';'1406';'4265';'5354';'8126'};
name_1 = {'Name';'N';'R';'A';'B'};
union_1 = [name_1,value_1];
Or sort 'name' alphabetically:
value_2 = {'Value';'5354';'8126';'1406';'4265'};
name_2 = {'Name';'A';'B';'N';'R'};
union_2 = [name_2,value_2];
I tried using respectively:
B = sortrows(union,2);
and this:
B = sortrows(union,1);
however, it also sorts me the first row that I do not want to move. Is there any way to lock the first row?
Or, once 'B' is calculated, move to the first row 'Value' & 'Name'.
  1 commentaire
Stephen23
Stephen23 le 26 Juin 2023
The best solution would be to use a table.

Connectez-vous pour commenter.

Réponses (1)

Fangjun Jiang
Fangjun Jiang le 26 Juin 2023
value = {'Value';'8126';'5354';'1406';'4265'};
name = {'Name';'B';'A';'N';'R'};
union = [name,value];
B = sortrows(union(2:end,:),1);
B=[union(1,:);B]
B = 5×2 cell array
{'Name'} {'Value'} {'A' } {'5354' } {'B' } {'8126' } {'N' } {'1406' } {'R' } {'4265' }
  1 commentaire
Alberto Acri
Alberto Acri le 26 Juin 2023
Modifié(e) : Alberto Acri le 26 Juin 2023
Thank you for the answer.
While in the case of the cell:
value = {'Value';'50';'80';'45';'66'};
name = {'Name';'B';'A';'B';'A'};
charact = {'Object';'home';'car';'money';'toys'};
union = [name,value,charact];
I would like to get this result:
value_3 = {'Value';'66';'80';'45';'50'};
name_3 = {'Name';'A';'A';'B';'B'};
charact_3 = {'Object';'toys';'car';'money';'home'};
union_3 = [name_3,value_3,charact_3];

Connectez-vous pour commenter.

Catégories

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

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by