2015-12-15 52 views
-2

你能幫助我嗎?我有矩陣,但我想要在矩陣中有更多的「fixed_pa​​rt」並隨機選擇。同樣重要的是每一行都是獨一無二的。你可以幫幫我嗎?在矩陣中隨機排序位

clear all 
clc 
ntags = 50; 
fixed_part = 20; 
IDlength = 64; 




tag_population = [zeros(ntags, fixed_part), floor(rand(ntags,IDlength-fixed_part)*2)]; 
tag_population = unique(tag_population,'rows'); 

查找這個矩陣:

0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 
0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 

在這個例子中我有四個不同類型的 「fixed_pa​​rt」 的。

+0

對不起,您所說的「更多fixed_pa​​rt」是什麼意思?更高的價值? – hbaderts

+0

@hbaderts,''fixed_pa​​rt''表示'0和1'隨機排序的組之間有多少'0'。我想要更多的'fixed_pa​​rt'在矩陣 – NikolaC

+0

我可能仍然理解你錯了(對不起,如果是這樣的話),但是你不能只將'fixed_pa​​rt'設置爲更高的數字。 30,得到那個? – hbaderts

回答

0

要與你想要的全矩陣回答:

for t = 1:size(A,1) 
    Y(t,:) = circshift(A(t,:)',randi([1 size(A,2)],1,1)') 

%//EDIT added first 4 digits cannot be 0 
    while sum(Y(t,1:4) > 0) 
    Y(t,:) = circshift(A(t,:)',2) 
    end 

%//Be warned, can run into infinite loop if the first 4 digits must contains at least a 1. 

end 

其中A是你的輸入矩陣。

Y =

0  0  0  0  1  1  0  0  0  0  1  0  0  0  0  0 
0  0  0  0  1  0  0  0  0  0  0  0  1  1  1  0 
0  0  0  0  0  1  1  1  0  0  0  0  0  0  0  0 
0  0  0  0  0  0  1  1  0  0  0  0  0  0  0  1 
0  0  0  0  0  0  0  0  0  1  0  1  0  0  0  0 
0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0 
0  0  0  0  0  0  1  0  0  0  0  1  1  0  0  0 
0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  0 
0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0 
0  0  0  0  0  1  1  1  0  0  0  0  0  0  0  0 

已經增加了 「更固定部分」 吧?

+0

這很好,但是我不應該在前四個職位上有一個。 – NikolaC

+0

@NikolaC我編輯了我的答案以包含變更 – GameOfThrows

+0

它是不斷的路徑。 Ii不工作 – NikolaC