How to order cell rows

조회 수: 1 (최근 30일)
Matlab User
Matlab User 2017년 1월 20일
답변: per isakson 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