如何在不改变第一列中元素顺序的情况下对 Matlab 中的多维矩阵的行进行排序?
How to sort the rows of a multidimensional matrix in Matlab without changing the order of the elements in the first column?
我有一个 3D 矩阵 A(i, j, k)。问题如下:
我有很多房间。我使用第一个维度(i's)来表示房间 ID。每个房间里都有几把椅子。我使用第二维(j)来表示椅子的 ID。每把椅子都有坐标 x,y,z。我使用第 3 维(k's)来表示坐标。
例如A(4,3,1)表示第4个房间,第3把椅子,x坐标; A(4,3,2)表示相同的房间和椅子但y坐标;和 A(4,3,3) z 坐标。
我需要根据其中一个维度,独立于其他房间对每个房间的椅子进行分类。
假设我只想对第一个房间的椅子进行排序,即 A(1 , : , :),根据它们的 x 坐标,即 A(1 , : , 1)。
任何人都可以帮助我如何在 Matlab 2016b 中做到这一点吗?
非常感谢!
我认为这符合您的要求:
A = randi(99,3,3,3); % example data
room = 1; % desired room
coord = 1; % desired coordinate
[~, ind] = sort(A(room,:,coord)); % get indices of the sorting
B = A; % result. Initiallize
B(room,:,:) = B(room,ind,:); % apply sorting to chairs in that room
我有一个 3D 矩阵 A(i, j, k)。问题如下:
我有很多房间。我使用第一个维度(i's)来表示房间 ID。每个房间里都有几把椅子。我使用第二维(j)来表示椅子的 ID。每把椅子都有坐标 x,y,z。我使用第 3 维(k's)来表示坐标。
例如A(4,3,1)表示第4个房间,第3把椅子,x坐标; A(4,3,2)表示相同的房间和椅子但y坐标;和 A(4,3,3) z 坐标。
我需要根据其中一个维度,独立于其他房间对每个房间的椅子进行分类。
假设我只想对第一个房间的椅子进行排序,即 A(1 , : , :),根据它们的 x 坐标,即 A(1 , : , 1)。
任何人都可以帮助我如何在 Matlab 2016b 中做到这一点吗?
非常感谢!
我认为这符合您的要求:
A = randi(99,3,3,3); % example data
room = 1; % desired room
coord = 1; % desired coordinate
[~, ind] = sort(A(room,:,coord)); % get indices of the sorting
B = A; % result. Initiallize
B(room,:,:) = B(room,ind,:); % apply sorting to chairs in that room