Borrar filtros
Borrar filtros

How to order cell rows

1 visualización (últimos 30 días)
Matlab User
Matlab User el 20 de En. de 2017
Respondida: per isakson el 20 de En. de 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.

Respuestas (1)

per isakson
per isakson el 20 de En. de 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

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by