필터 지우기
필터 지우기

Sorting data points so x are in ascending order.

조회 수: 48 (최근 30일)
David
David 2013년 12월 3일
편집: Viktor 2013년 12월 3일
I have two vectors x and y and they correspond to a given set of data points. How can I sort x in ascending order while at the same time, sorting y so that the values of y match up with their corresponding values of x?? Eg. (1,2) (3, 0) (-4, 6) (-2, 5) would yield vectors x = [1,3,-4,-2] and y = [2,0,6,5] then the sorted vectors would be x = [-4,-2,1,3] and y = [6,5,2,0].

답변 (3개)

Jos (10584)
Jos (10584) 2013년 12월 3일
편집: Jos (10584) 2013년 12월 3일
Use the second output of SORT
% example data
X = [1 3 -4 -2]
Y = [2 0 6 5]
[sortedX, sortIndex] = sort(X)
sortedY = Y(sortIndex)
Another option would be via SORTROW

Viktor
Viktor 2013년 12월 3일
편집: Viktor 2013년 12월 3일
You have a sortrows() function that you can use. Assuming you have two row vectors:
sorted = (sortrows([x',y'], 1))'
% reassigning sorted values
x = sorted(1,:);
y = sorted(2,:);
I just noticed that Jos has already mentioned, I left my answer here, it might be useful.

Andrei Bobrov
Andrei Bobrov 2013년 12월 3일
[x1,ii]= sort(x);
y1 = y(ii);

카테고리

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