block first row of a cell

1 次查看(过去 30 天)
Alberto Acri
Alberto Acri 2023-6-26
评论: Stephen23 2023-6-26
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 个)

Fangjun Jiang
Fangjun Jiang 2023-6-26
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 个评论
Alberto Acri
Alberto Acri 2023-6-26
编辑:Alberto Acri 2023-6-26
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];

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by