Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 我想生成(或计算)满足一定条件的所有可能的二进制矩阵_Matlab - Fatal编程技术网

Matlab 我想生成(或计算)满足一定条件的所有可能的二进制矩阵

Matlab 我想生成(或计算)满足一定条件的所有可能的二进制矩阵,matlab,Matlab,我想生成(或计数)满足以下条件的所有可能的二进制矩阵。 设A为任意二元矩阵4*4 暴力法:生成所有4x4二进制矩阵,并测试其中哪一个符合您的条件: condition = [2 2 2 2 2 2 2 2]; %// desired conditions matrices = reshape(dec2bin(0:2^16-1,16).'-'0', 4,4,[]); %'// all binary matrices ind = all(bsxfun(@eq, [squeeze(sum(matri

我想生成(或计数)满足以下条件的所有可能的二进制矩阵。 设A为任意二元矩阵4*4


暴力法:生成所有4x4二进制矩阵,并测试其中哪一个符合您的条件:

condition = [2 2 2 2 2 2 2 2]; %// desired conditions

matrices = reshape(dec2bin(0:2^16-1,16).'-'0', 4,4,[]); %'// all binary matrices
ind = all(bsxfun(@eq, [squeeze(sum(matrices,1)); squeeze(sum(matrices,2))],...
  condition(:))); %// gives 1 for matrices that fulfill the condition, or else 0
result = matrices(:,:,ind); %// pick solution matrices
number = size(result,3); %// number of solution matrices
解矩阵是
结果(,:,1)
结果(,:,2)
。。。;
number
是解矩阵的个数


你可以利用对称性来加快速度。

你有或想要公式/算法吗?谢谢路易斯·门多,但它不适用于20*20或更高的大尺寸,因为它应该生成非常非常。。。矩阵和…@user3375253您应该从一开始就指定需要使用的矩阵大小。不管怎么说,我想不出任何实质上不是暴力的方法,对不起
condition = [2 2 2 2 2 2 2 2]; %// desired conditions

matrices = reshape(dec2bin(0:2^16-1,16).'-'0', 4,4,[]); %'// all binary matrices
ind = all(bsxfun(@eq, [squeeze(sum(matrices,1)); squeeze(sum(matrices,2))],...
  condition(:))); %// gives 1 for matrices that fulfill the condition, or else 0
result = matrices(:,:,ind); %// pick solution matrices
number = size(result,3); %// number of solution matrices