How can I sort my data by within a sorting

조회 수: 3 (최근 30일)
John Hunt
John Hunt 2017년 10월 10일
편집: dpb 2017년 10월 10일
I have a set of x,y,z data. I want to have all of the data sorted by x (least to greatest), then I want to have the y data be sorted within the x sort (least to greatest in the positive x data and then least to greatest for the negative data). I then want to do the same in the same for the z data in the positive and negative y data. How can I do that?
  댓글 수: 1
John Hunt
John Hunt 2017년 10월 10일
The answers below are almost what I need, this sorts the data by the last column, so I have positive number with negative numbers in the -x numbers. I need them separate.
so -x,y -x,-y -x,-y -x,-y -x,y x,y x,-y x,-y x,y x,y to --> -x,-y -x,-y -x,-y -x,y -x,y x,-y x,-y x,y x,y x,y

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

답변 (2개)

Cedric
Cedric 2017년 10월 10일
[xyz_sorted, id] = sortrows( [x(:),y(:),z(:)] ) ;
  댓글 수: 3
Cedric
Cedric 2017년 10월 10일
You will have to make a numeric example.
dpb
dpb 2017년 10월 10일
Ewww...that's taking some doing but isn't too bad; just bookkeeping.
  1. sort x keeping index vector, iSrt
  2. get logical vector for y of negative locations, iNegY
  3. build sorted row in pieces of [x(iSrt(iNegY)) y(iNegY)]
  4. concatenate the remainder with [x(iSrt(~iNegY)) y(~iNegY)]
Is then complicated that there's the subsequent partitioning of Z as subset of Y to keep those pieces so in reality there's four subsections, not just two (or, of course, if you also mean this for X, then it's 8).
I've got another appointment at moment, but that's the idea...

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


dpb
dpb 2017년 10월 10일
편집: dpb 2017년 10월 10일
doc sortrows
Following up on previous note -- given
>> x=rand(10,1); y=rand(size(x))-0.5;
>> [x y]
ans =
0.1576 0.1557
0.9706 -0.4643
0.9572 0.3491
0.4854 0.4340
0.8003 0.1787
0.1419 0.2577
0.4218 0.2431
0.9157 -0.1078
0.7922 0.1555
0.9595 -0.3288
>>
for the x,y case
>> [~,iSrt]=sort(x);
>> iNegY=y<0;
>> [sortrows([x(iSrt(iNegY)) y(iNegY)]); ...
sortrows([x(iSrt(~iNegY)) y(~iNegY)])]
ans =
0.1576 -0.4643
0.9572 -0.1078
0.9706 -0.3288
0.1419 0.1557
0.4218 0.3491
0.4854 0.4340
0.7922 0.1787
0.8003 0.2577
0.9157 0.2431
0.9595 0.1555
>>
Presuming that's the desired result, have to subsequently partition z by the two subsections within y for total of four pieces.

카테고리

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