How can I sort my data by within a sorting

8 ビュー (過去 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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by