在Matlab中的列向量中查找一组数字的最小值
Finding minimum value of a set of numbers in a column vector in Matlab
我试图在作为矩阵一部分的列向量中找到一组数字的最小值。更具体地说,如果我有如下矩阵,
1 2 3 4 2.7
7 2 3 8 2.3
5 2 3 9 12.5
10 4 5 12 1.1
11 4 5 13 5.6
14 5 6 7 1.2
15 5 6 8 0.5
16 5 6 9 3.4
17 5 6 12 6.8
那么,我希望输出如下,
7 2 3 8 2.3
10 4 5 12 1.1
15 5 6 8 0.5
我想过根据第2列和第3列是否相同来划分矩阵,然后在每个子矩阵中找到第5列的最小值,但我无法有效地实现它。 (编辑:我尝试做的是,我首先将矩阵的第 2 列和第 3 列分开(比如 A),然后我使用 unique 来获取重复行的出现次数,如下所示,
A_dup = A(:,2:3)
[A_uni,~,u_id] = unique(A_dup,'rows')
num_occur = histc(u_id, unique(u_id))
向量'num_occur' 将告诉我如何将矩阵 A 划分为子矩阵。这并不多,但我在这之后被卡住了。)
如果有更好的方法,我会很高兴知道。
使用 sortrows
and get the indices of the sorted elements. Find the row subscripts of the unique
rows of the 2nd and 3rd columns of the new matrix . Use these row subscripts with the indices found before to get the row subscripts to be kept from the original matrix. Sort
根据第 5 列的元素对矩阵进行排序以保持顺序。使用排序后的新行下标得到需要的结果。
[req, idxA] = sortrows(A, 5);
[~, idxtmpA] = unique(req(:,2:3), 'rows');
req = A(sort(idxA(idxtmpA)), :);
我试图在作为矩阵一部分的列向量中找到一组数字的最小值。更具体地说,如果我有如下矩阵,
1 2 3 4 2.7
7 2 3 8 2.3
5 2 3 9 12.5
10 4 5 12 1.1
11 4 5 13 5.6
14 5 6 7 1.2
15 5 6 8 0.5
16 5 6 9 3.4
17 5 6 12 6.8
那么,我希望输出如下,
7 2 3 8 2.3
10 4 5 12 1.1
15 5 6 8 0.5
我想过根据第2列和第3列是否相同来划分矩阵,然后在每个子矩阵中找到第5列的最小值,但我无法有效地实现它。 (编辑:我尝试做的是,我首先将矩阵的第 2 列和第 3 列分开(比如 A),然后我使用 unique 来获取重复行的出现次数,如下所示,
A_dup = A(:,2:3)
[A_uni,~,u_id] = unique(A_dup,'rows')
num_occur = histc(u_id, unique(u_id))
向量'num_occur' 将告诉我如何将矩阵 A 划分为子矩阵。这并不多,但我在这之后被卡住了。)
如果有更好的方法,我会很高兴知道。
使用 sortrows
and get the indices of the sorted elements. Find the row subscripts of the unique
rows of the 2nd and 3rd columns of the new matrix . Use these row subscripts with the indices found before to get the row subscripts to be kept from the original matrix. Sort
根据第 5 列的元素对矩阵进行排序以保持顺序。使用排序后的新行下标得到需要的结果。
[req, idxA] = sortrows(A, 5);
[~, idxtmpA] = unique(req(:,2:3), 'rows');
req = A(sort(idxA(idxtmpA)), :);