How to sort 2 matching column only decending one of them?

7 visualizaciones (últimos 30 días)
dj
dj el 11 de Dic. de 2018
Comentada: dj el 11 de Dic. de 2018
I have matching period and height datas for wave analysis. I'm trying to sort wave height in descending order and match every individual period with its wave height. How can i do that?
  2 comentarios
Jan
Jan el 11 de Dic. de 2018
Please post, which inputs you have in valid Matlab terms. Which class and dimensions? The physical meaning does not matter. What do you want to sort and what does "match every individual period with its wave height" mean? Can you post a minimal example of the inputs and wanted outputs?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Dic. de 2018
Editada: Stephen23 el 11 de Dic. de 2018
Either put the data into one matric and sort by one column, e.g.:
M = [T(:),H(:)]; % [period,height]
M = sortrows(M,-2) % sort rows by second column descending
Or sort one vector and then use the indices to sort the other:
[H,idx] = sort(H,'descend')
T = T(idx)

Más respuestas (1)

Mark Sherstan
Mark Sherstan el 11 de Dic. de 2018
Something along these lines should do it:
T = randi(10,100,1); % Random period
H = randi(20,100,1); % Random height
data = [H T];
dataSorted = sortrows(data,'descend')
  2 comentarios
dj
dj el 11 de Dic. de 2018
But i only want to sort H in descend order and code to rearrange positions of T for its H in the first place. How can i do that?
Mark Sherstan
Mark Sherstan el 11 de Dic. de 2018
What do you mean by code to rearranage position T for its H? When you use sortrows it keeps all the data together. As H and T are in the same matrix the data indices stay matched.

Iniciar sesión para comentar.

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