Clear Filters
Clear Filters

block first row of a cell

1 view (last 30 days)
Alberto Acri
Alberto Acri on 26 Jun 2023
Commented: Stephen23 on 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 Comment
Stephen23
Stephen23 on 26 Jun 2023
The best solution would be to use a table.

Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 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 Comment
Alberto Acri
Alberto Acri on 26 Jun 2023
Edited: Alberto Acri on 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];

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!