How to sort 2 matching column only decending one of them?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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?
Stephen23
el 11 de Dic. de 2018
Respuesta aceptada
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
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
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.
Ver también
Categorías
Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!