Sorting the vector y wrt the vector x?

조회 수: 18 (최근 30일)
MEET SHAH
MEET SHAH 2022년 4월 16일
답변: Image Analyst 2022년 4월 17일
I have two vectors x and y
x = 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.66
y = 0.051998 0.084698 0.117398 0.150098 0.182798 0.215498 0.248198 0.19 0.09 0.235118
I want to make the matrix [x,y] in such a way that x is in ascending, i.e., results should be
x = 0.1 0.2 0.3 0.4 0.5 0.6 0.66 0.7 0.8 0.9
y = 0.051998 0.084698 0.117398 0.150098 0.182798 0.215498 0.235118 0.248198 0.19 0.09
Any suggestions are apreciated and thankyou in advance for your help !!

채택된 답변

Voss
Voss 2022년 4월 16일
x = [ 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.66 ];
y = [ 0.051998 0.084698 0.117398 0.150098 0.182798 0.215498 0.248198 0.19 0.09 0.235118 ];
[~,idx] = sort(x);
M = [x(idx); y(idx)]
M = 2×10
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.6600 0.7000 0.8000 0.9000 0.0520 0.0847 0.1174 0.1501 0.1828 0.2155 0.2351 0.2482 0.1900 0.0900

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 4월 17일
Slightly different but essentially the same
x = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.66];
y = [0.051998, 0.084698, 0.117398, 0.150098, 0.182798, 0.2154908, 0.248198, 0.19, 0.09, 0.235118];
% Sort (and replace) x and get the order in which it was sorted.
[x, sortOrder] = sort(x, 'ascend')
x = 1×10
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.6600 0.7000 0.8000 0.9000
sortOrder = 1×10
1 2 3 4 5 6 10 7 8 9
% Sort y using the same order as x used, replacing original y with new, sorted y.
y = y(sortOrder)
y = 1×10
0.0520 0.0847 0.1174 0.1501 0.1828 0.2155 0.2351 0.2482 0.1900 0.0900

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by