"Sort 3_D array along the third dimension"
1 回表示 (過去 30 日間)
古いコメントを表示
https://www.mathworks.com/help/matlab/ref/sort.html has an example of "Create a 2-by-2-by-2 array and sort its elements in ascending order along the third dimension." What does "along the third dimension" mean?
1 件のコメント
採用された回答
Steven Lord
2022 年 7 月 15 日
If you look at the pictures in the description of the dim input argument on that page, note that the arrows go down along the columns (for dimension 1) and across the rows (for dimension 2.) If we had a similar picture for dimension 3, the arrows would be poking out of the screen.
Another analogy: take a deck of playing cards and deal out 24 cards in 6 stacks (two rows and three columns) with 4 cards in each stack. Sorting that 2-by-3-by-4 array along the third dimension would require sorting each stack of 4 cards independently, without moving any cards to a different stack.
rng default % for reproducibility
x = reshape(randperm(24), [2 3 4])
s = sort(x, 3)
The stack s(2, 1, :) has 2 in the first page, 5 in the second, 16 in the third, and 20 in the fourth. In x, those elements were arranged as {16, 5, 2, 20} instead.
Based on something you asked in a different question, I think you expected this to sort a matrix by its third column. If so, that's the type of problem you'd use sortrows for.
B = reshape(randperm(12), [3 4])
Bsorted = sortrows(B, 2)
The second column of B is [6; 9; 4]. When you sort B by that column the third row of B becomes the first row of Bsorted, the first row of B becomes the second row of Bsorted, and the second row of B becomes the third row of Bsorted.
0 件のコメント
その他の回答 (1 件)
Torsten
2022 年 7 月 15 日
It means that B(:,:,1) <= B(:,:,2) is arranged by ordering the vectors [2,-1], [3,9],[1,0] and [6,12].
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!