How to order cell rows

1 次查看(过去 30 天)
Matlab User
Matlab User 2017-1-20
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.

回答(1 个)

per isakson
per isakson 2017-1-20
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

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by