創建

2016-09-08 54 views
-2

我已產生不同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 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矩陣沒有足夠一和零,然後我想重新從我的代碼master矩陣。

+1

我猜你需要總和**行**,而不是列,因爲主人的大小是21列,你想要的總和是21個元素。創建新的矩陣 - 應用什麼規則? – Rotem

+0

我需要總結這一點我確信這一點。我需要的主人的大小是21列,所以我把我需要的每列的總和21個數字 –

+0

總和列:'sum(master,2)'。所有的值都是6,並且只有17個元素......(它們實際上是不同數量的元素)。 – Rotem

回答

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