How to order cell rows

1 view (last 30 days)
Matlab User
Matlab User on 20 Jan 2017
Answered: per isakson on 20 Jan 2017
I have a large cell (174x4 cell). I've attached a smaller working example. This takes the form of x for Copy(:,1), y for Copy(:,2) , ID for Copy(:,3) and a weight for Copy(:,4). I would like to for each row, sort by ascending x order i.e. what is in Copy(i,1) for i=3 rows in my small example. I want to retain the pairings of y, ID and weight. So sort only by x. Is this possible for a cell like so?
Many thanks.

Answers (1)

per isakson
per isakson on 20 Jan 2017
Copy =
[ 2x1 double] [ 2x1 double] [ 2x1 double] [ 2x1 double]
[33x1 double] [33x1 double] [33x1 double] [33x1 double]
[20x1 double] [20x1 double] [20x1 double] [20x1 double]
Try this function
function sorted_copy = cssm( Copy )
sorted_copy = cell( size( Copy ) );
for rr = 1 : size(Copy,1) % for each row
[~,ixs] = sort( Copy{rr,1}, 'ascend' );
for cc = 1 : size(Copy,2) % retain the pairings
sorted_copy{rr,cc} = Copy{rr,cc}(ixs);
end
end
end

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!