Problems with sortrows function

4 ビュー (過去 30 日間)
Milan Mihajlovic
Milan Mihajlovic 2024 年 6 月 5 日
回答済み: Milan Mihajlovic 2024 年 6 月 6 日
I am trying to sort a two dimensional matrix Coord consisting of (x,y) pairs of coordinates (of size n x 2) in lexicographical order, with respect to x and then y. I used the command
[Coord_Lex,ix]=sortrows(Coord,[1,2]);
This should sort x coordinates in ascending order and within each set of matrix rows with the same x coordinate the y coordinates in ascending order. But in some cases it does not work correctly. For example, a part of the answer that I am getting is
1.2250 0.2067
1.2250 0.1722
1.2250 0.1378
1.2250 0.1033
1.2250 0.0689
1.2250 0.0344
1.2250 -0.2933
1.2250 -0.3278
1.2250 -0.3622
1.2250 -0.3967
1.2250 -0.4311
i.e., the y coordinaes are not sorted correctly. I checked the values of x coordinates in the incorrect part for the round-off error polution and the difference between them always gives 0.
  2 件のコメント
Eric
Eric 2024 年 6 月 5 日
What version of MATLAB are you using? I just tried this in R2023b, and it worked fine for me.
Steven Lord
Steven Lord 2024 年 6 月 5 日
Can you attach a small sample of data with which you can reproduce this behavior to your post, or perhaps display it using format hex so we can see the exact values that are stored in your variables? I suspect there's a difference in the last bit that's preventing sortrows from behaving as you think it should (even though it is behaving as it is designed to do.)
format % Show a few decimal places
p = pi
p = 3.1416
format longg % Show more decimal places
p = pi
p =
3.14159265358979
format hex % Show the actual bits stored in memory
p = pi
p =
400921fb54442d18

サインインしてコメントする。

回答 (1 件)

Milan Mihajlovic
Milan Mihajlovic 2024 年 6 月 6 日
Dear Steven,
this seems to be the case for some nodes. I created the matrix of differences between the x-coordinates of the nodes that "misbehaved" and in some cases the difference is equal to a round-off of 2.2e-16 which does cause the problem. Thank you for your insight. Best wishes, Milan

カテゴリ

Help Center および File ExchangeShifting 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