我有以下示例代码生成不同的master
矩阵,每次运行时只包含0&1和1'
R=[];
Repeted=50;
Person=10;
for i=1:Repeted
R1=randi([0,1],[Person,21]);
R= [R;R1];
end
sumrows=find(sum(R(1:end,1:end),2)==6);
if sumrows
B=R(sumrows,:);
end
master= unique(B,'rows')
现在我需要从master
矩阵中随机选择一些元素并创建一个包含以下行总和的10行和21列的新矩阵:
[10 5 3 6 6 4 5 8 5 9 5 5 5 4 4 4 3 7 4 4 5]
如果master
矩阵的元素无法使用规定的总和,那么我想重新创建master
矩阵。
例如,如果我的代码生成了以下master
矩阵:
master = [ 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 ;
0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 ;
0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 1 ;
0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 0 1 0 0 0 0 ;
0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 1 ;
0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0 1 ;
0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 ;
0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 1 ;
0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 ;
0 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 ;
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 1 0 1 ;
0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 0 0 ;
0 1 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 ;
0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 ;
0 1 1 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 ;
0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 ;
1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 1 0 0 0 0 ;
1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 ;
1 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 ;
1 1 0 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 ]
由于master
矩阵具有足够数量的1和0,因此可以生成新矩阵。所以新矩阵可以如下所示:
[ 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 ;
1 1 0 0 0 1 0 0 1 0 1 1 1 0 0 0 1 1 0 0 1 ;
1 1 0 0 0 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 ;
1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 0 1 1 1 ;
1 0 1 1 1 0 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 ;
1 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 0 0 0 ;
1 0 0 1 0 0 1 1 0 1 1 1 1 0 0 0 0 1 1 1 1 ;
1 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 ;
1 1 0 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 1 ;
1 1 0 1 1 0 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 ]
但如果master
矩阵没有足够的1和0,那么我想从我的代码中重新创建master
矩阵。
答案 0 :(得分:0)
<强>代码: - 强>
我没有对您已编写的代码进行任何更改。完整的代码和解释为注释:
Repeted = 50; Person = 10;
NewMat = zeros(10,21); % Initializing 'NewMat' with all zeros
SRNewMat=[10 5 3 6 6 4 5 8 5 9 5 5 5 4 4 4 3 7 4 4 5]; %Sum of the rows as mentioned by you
SSRNewMat=sum(SRNewMat); % Sum of 'SRNewMat' Matrix
while(1)
R = [];
for i = 1:Repeted
R1 = randi([0,1],[Person,21]);
R = [R;R1];
% In the above step, you are growing the size of 'R' in a loop
% which is not a good programming practice. Pre-allocation should
% be done instead but I am leaving the code, that you wrote, as it is
end
sumrows = find(sum(R(1:end,1:end),2)==6);
if sumrows
B = R(sumrows,:);
end
Master = unique(B,'rows');
if (sum(Master(:)) >= SSRNewMat) && ((numel(Master)-sum(Master(:)))>= (numel(NewMat)-SSRNewMat))
% Checking if 'master' has required number of ones & required number of zeros
for k = 1:21 % 21 is basically size(NewMat,2) i.e. no. of columns of NewMat
NewMat(1:SRNewMat(k),k) = ones(SRNewMat(k),1); %Replacing 0's with 1's in NewMat
NewMat(:,k)= NewMat(randperm(10),k); %Shuffling the rows of kth column of NewMat
%In the above line, 10 is basically size(NewMat,1) i.e. no. of rows of NewMat
end
break; %No need to run again if mentioned sum is possible
end
end
NewMat
的输出: - 强>
我运行此代码时生成的输出之一如下所示:
NewMat =
1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 0 1 1
1 1 1 0 1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1
1 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 1
1 0 0 1 1 1 1 1 0 1 1 1 1 0 0 1 0 1 0 1 0
1 0 0 1 0 0 1 0 1 1 1 0 1 0 0 0 0 1 1 0 1
1 1 0 0 1 1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0
1 0 0 0 0 0 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 1 0 0
1 1 0 1 1 1 1 1 1 1 1 0 0 0 1 0 1 0 0 1 0
1 0 1 0 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1