Filter löschen
Filter löschen

block first row of a cell

1 Ansicht (letzte 30 Tage)
Alberto Acri
Alberto Acri am 26 Jun. 2023
Kommentiert: Stephen23 am 26 Jun. 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 Kommentar
Stephen23
Stephen23 am 26 Jun. 2023
The best solution would be to use a table.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Fangjun Jiang
Fangjun Jiang am 26 Jun. 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 Kommentar
Alberto Acri
Alberto Acri am 26 Jun. 2023
Bearbeitet: Alberto Acri am 26 Jun. 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];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by