句点上的随机数(使用逻辑)

时间:2011-11-22 09:05:27

标签: matlab random matrix

使用以下代码:

t=10;
period=9;
A=zeros(36,36,t);
for j=1:t
   A(7:period:end,7:period:end,j)=rand(1,1);
   A(8:period:end,8:period:end,j)=rand(1,1);
   A(9:period:end,9:period:end,j)=rand(1,1);
end

编辑:这是A.我已将它们分为9x9,因此更容易看到模式。

0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 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 1 0 0   0 0 0 0 0 0 1 0 0   0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0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 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 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 1 0 0   0 0 0 0 0 0 1 0 0   0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 0 1

我想制作标记为1的每个数字,一个0到1之间的随机数。是否需要创建一个向量(48,1),然后插入1所在的值?


当然,这些数字并非完全随机,每个“期间”都会重现相同的三个随机数。

是否有一种简单的方法可以创建随机数,因此每个随机生成的值都是随机的?

非常感谢。

1 个答案:

答案 0 :(得分:2)

起初比我想象的要复杂一些。这是我的解决方案:

A = zeros(36,36,t);
idx = sub2ind([36 36 t],repmat(34:36,1,t),repmat(34:36,1,t),repmat(1:t,1,3));
A(idx) = rand(length(idx),1);

具有逻辑索引的解决方案是:

A(logical(A)) = rand(nnz(A),1);

但是你必须准备A作为问题中给出的二进制矩阵。