"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!