我有以下示例代码,每次运行时生成不同的master
矩阵,仅由0和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
矩阵。
代码:-
我没有对你已经写好的代码做任何改变。完整的代码和注释是:
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