matlab - 寻找具有最小元素总和的子矩阵

标签 matlab matrix

我有一个对称的 m-by-m 矩阵 A。每个元素都有一个介于 0 和 1 之间的值。我现在想选择 An 行/列,它们形成 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/

相关文章:

matlab - 下载后如何安装SeDuMi for YALMIP matlab?

r - 当矩阵的维数未知时,如何设置唯一的行名和列名?

python - 如何在 SymPy 中扩展矩阵表达式?

c++ - 如何使用线性代数的C++模板库Eigen?

matlab - 在Matlab中比较两个矩阵

matlab - 关于浮点精度 : why the iteration numbers are not equal?

matlab - 在 MATLAB 中优化手动编码的 k 均值?

c++ - 使用 SWIG 对 C++ 库进行 Matlab 绑定(bind)

matlab - 补丁透明度问题 (FaceAlpha)

c++ - 解决PnP : Obtaining the rotation translation matrix