矩阵中的随机排序位

时间:2015-12-15 14:53:15

标签: matlab

你能帮助我吗?我有矩阵,但我想在矩阵中有更多的“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”。

1 个答案:

答案 0 :(得分: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

“更多修复部分”已经增加了吗?