Azzera filtri
Azzera filtri

block first row of a cell

1 visualizzazione (ultimi 30 giorni)
Alberto Acri
Alberto Acri il 26 Giu 2023
Commentato: Stephen23 il 26 Giu 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 Commento
Stephen23
Stephen23 il 26 Giu 2023
The best solution would be to use a table.

Accedi per commentare.

Risposte (1)

Fangjun Jiang
Fangjun Jiang il 26 Giu 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 Commento
Alberto Acri
Alberto Acri il 26 Giu 2023
Modificato: Alberto Acri il 26 Giu 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];

Accedi per commentare.

Categorie

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

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by