Best way to sort a 3-d matrix by one column?
조회 수: 17 (최근 30일)
이전 댓글 표시
Hi, I'd like to sort a 3 d matrix by the values in one column. I have a 204 x 33 x 9 matrix, and I'd like to sort the rows by the 33rd column of the second dimension. Thanks, Emily
댓글 수: 2
답변 (2개)
gonzalo Mier
2018년 10월 11일
I am not sure if this is what you want:
matrix(:,33,2) = sort(matrix(:,33,2));
댓글 수: 3
Guillaume
2018년 10월 15일
You can't use sortrows on a 3D matrix. You have two options:
- Use a loop and use sortrows on the pages:
for page = 1:size(yourmatrix, 3)
yourmatrix(:, :, page) = sortrows(yourmatrix(:, :, page), 33);
end
- Use sort and some sub2ind magic to sort everything at once. I'm not sure it'll be faster than the loop:
[~, roworder] = sort(yourmatrix(:, 33, :));
sortedmatrix = yourmatrix(sub2ind(size(yourmatrix), ...
repmat(roworder, 1, size(yourmatrix, 2), 1), ...
repmat(1:size(yourmatrix, 2), size(yourmatrix, 1), 1, size(yourmatrix, 3)), ...
repmat(permute(1:size(yourmatrix, 3), [1 3 2]), size(yourmatrix, 1), size(yourmatrix, 2), 1)));
댓글 수: 0
참고 항목
카테고리
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!