Sorting 2 Arrays for 2D Graphs

조회 수: 10 (최근 30일)
Robert Dominski
Robert Dominski 2018년 2월 25일
댓글: Star Strider 2018년 2월 26일
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,

채택된 답변

Star Strider
Star Strider 2018년 2월 25일
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))

추가 답변 (1개)

Robert Dominski
Robert Dominski 2018년 2월 26일
Thanks! I'll give this a shot and update with a check for ya. Thanks a bunch! Didn't even think of that.
Cheers!
  댓글 수: 1
Star Strider
Star Strider 2018년 2월 26일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by