【发布时间】:2016-12-21 19:53:34
【问题描述】:
我有以下函数用于分散矩阵中 1 的数量,如果存在全 0 或全 1 的行,则必须删除该特定行
function ReducedMatrix = ReduceMatrix(result)
D1 = sum(result(:));
NumberOfOnes = floor(D1*0.3);
NewMatrix = zeros(size(result));
NewMatrix(randi(numel(NewMatrix),1,NumberOfOnes)) = 1;
ReducedMatrix = NewMatrix;
while numel(ReducedMatrix)/numel(NewMatrix) > 0.2
IndexOfFullRows = find(all(ReducedMatrix));
if isempty(IndexOfFullRows)
break
end
ReducedMatrix(:,IndexOfFullRows(1)) = [];
end
end
函数的输入和输出如下
result =
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 0 1 1
1 1 0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 0 1 0 1
1 0 1 1 1 1 1 0 1 1
1 1 1 1 0 1 1 1 0 1
1 0 1 1 1 0 1 1 1 1
1 1 1 1 0 1 0 1 1 1
1 1 1 0 1 1 1 1 1 1
ReducedMatrix =
0 1 1 0 0 0 0 0 1 0
0 1 0 0 0 0 0 1 0 0
1 1 1 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 1 1
1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 1
row_sum =
3
2
3
2
1
4
2
2
0
3
col_sum =
3 4 4 1 0 0 3 2 2 3
现在,如果存在 row_sum/col_sum 等于 0 或 1 的行或列,则必须删除相应的行。 例如。 Row-R4,R9 和 Col-C4,C5,C6 的 row_sum 和 col_sum 为 1,0。所以将它们加起来 R4,R9,C4,C5,C6 = 5 行必须从矩阵中消除,所以我的简化矩阵的大小应该是 5x5。请注意,不应删除列,而不是删除具有 0 和 1 的列,而是可以删除相应的行。同样,此函数必须针对具有相同约束的较大矩阵运行。我尝试执行上述功能,但是我没有足够的技能来达到我想要的结果,非常感谢任何帮助
【问题讨论】:
-
那么,如果现在删除一行会改变列总和并使其现在必须删除另一列,会发生什么?
-
我在函数中做了一个限制,不能超过 30% 的行可以减少
标签: matlab networking matrix