如何根据列中的值分隔数组?

How do I separate an array depending on values in a column?

假设我有一个 Mx4 数组 A,其中第一列中的值为 1 到 12 的数字。现在我想根据第 1 列中的数字将其余列收集到 12 个单独的 Mx3 数组中。 我该怎么做?

您可以使用 unique and splitapply as follows. The result is a cell array 个数组。

M = [2 11 41 51;
     1 10 20 30;
     1 62 83 22;
     4 73 53 53;
     2 84 94 14]; % example data
L = 5; % Group labels are 1:L (L=12 in your case)
[u,~,w] = unique(M(:,1));
result = cell(L,1);
result(u) = splitapply(@(x){x}, M(:,2:end), w);

这给了

>> celldisp(result)
result{1} =
    10    20    30
    62    83    22
result{2} =
    11    41    51
    84    94    14
result{3} =
     []
result{4} =
    73    53    53
result{5} =
     []