Sorting 2 Arrays for 2D Graphs

10 visualizaciones (últimos 30 días)
Robert Dominski
Robert Dominski el 25 de Feb. de 2018
Comentada: Star Strider el 26 de Feb. de 2018
Hey:
I have a program that allows input to calculate the friction forces given a mass ratio. After every iteration I have it place these values into an array to be plotted afterwards. When it plots, it draws the line according to the input in the array. I want to sort the array from lowest to highest, but unsure how to link both arrays.
For example: A = [.5, .2, .7, 1], B = [200, 300, 400, 500], plot(A,B)
This would start drawing the graph from the first point and connecting the points in the order of the array. I want to convert A and B such that:
A = [.2, .5, .7, 1], B = [300, 200, 400, 500]
How would I do this?
Cheers,

Respuesta aceptada

Star Strider
Star Strider el 25 de Feb. de 2018
Probably the easiest way is to concatenate ‘A’ and ‘B’ into one array, then use the sortrows function:
A = [.5, .2, .7, 1];
B = [200, 300, 400, 500];
ABs = sortrows([A(:) B(:)]);
plot(ABs(:,1), ABs(:,2))

Más respuestas (1)

Robert Dominski
Robert Dominski el 26 de Feb. de 2018
Thanks! I'll give this a shot and update with a check for ya. Thanks a bunch! Didn't even think of that.
Cheers!

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by