我有一个对称的 m
-by-m
矩阵 A
。每个元素都有一个介于 0 和 1 之间的值。我现在想选择 A
的 n
行/列,它们形成 n
-by-n
子矩阵 B
.
选择这些元素的标准是 B
所有元素的总和必须是所有可能的 n
-by-n< 中的最小值
A
的子矩阵。
例如,假设 A
是一个 4×4 矩阵:
A = [0 0.5 1 0; 0.5 0 0.5 0; 1 0.5 1 1; 0 0 1 0.5]
并且n
设置为3。那么,最好的B
就是取A
的第一、二、四行/列的那个>:
B = [0 0.5 0; 0.5 0 0; 0 0 0.5]
其中这些元素的总和为 0 + 0.5 + 0 + 0.5 + 0 + 0 + 0 + 0 + 0.5 = 1.5,它小于另一个可能的 3×3 子矩阵(例如使用第一个,第三和第四行/列)。
我该怎么做?
这部分是数学问题,部分是 Matlab 问题。任何帮助都会很棒!
最佳答案
执行以下操作:
m = size(A,1);
n=3;
sub = nchoosek(1:m,n); % (numCombinations x n)
subR = permute(sub,[2,3,1]); % (n x 1 x numCombinations), row indices
subC = permute(sub,[3,2,1]); % (1 x n x numCombinations), column indices
lin = bsxfun(@plus,subR,m*(subC-1)); % (n x n x numCombinations), linear indices
allB = A(lin); % (n x n x numCombinations), all possible Bs
sumB = sum(sum(allB,1),2); % (1 x 1 x numCombinations), sum of Bs
sumB = squeeze(sumB); % (numCombinations x 1), sum of Bs
[minB,minBInd] = min(sumB);
fprintf('Indices for minimum B: %s\n',mat2str(sub(minBInd,:)))
fprintf('Minimum B: %s (Sum: %g)\n',mat2str(allB(:,:,minBInd)),minB)
这只查找行索引与列索引相同且不一定连续的子矩阵。我就是这样理解这个问题的。
关于matlab - 寻找具有最小元素总和的子矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020885/