Azzera filtri
Azzera filtri

How to order cell rows

1 visualizzazione (ultimi 30 giorni)
Matlab User
Matlab User il 20 Gen 2017
Risposto: per isakson il 20 Gen 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.

Risposte (1)

per isakson
per isakson il 20 Gen 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

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by