matlab - 用 block 状或聚合的方法随机替换矩阵中的元素

标签 matlab random matrix replace

在下面的 MATLAB 代码中,矩阵 B 是通过将矩阵 A 的一部分元素 (empty_x) 随机替换为6。不是在整个矩阵中随机替换 empty_x 元素的值,而是如何让它们成 block (但仍然随机放置成 block )?

也就是说,我希望所有 6 值元素都相邻。如果将方阵A 中的empty_x 元素替换为较小的子集化方阵(大小empty_x)以创建矩阵B 那么就可以了。拥有并不总是方形矩阵(即异质性)的团 block 会很酷,但不是必需的。

我会很感激关于如何完成这项任务的一些想法。

干杯。

A = [1 2 3 4 5; 5 4 3 2 1; 1 4 3 2 5; 4 3 2 1 5; 2 1 3 5 4];
B = A;
nA = numel(A);
empty_x = randi(10);
B(randperm(nA,(empty_x))) = 6;

最佳答案

我的方法如下:

1) Generate a single random number (uniform distribution)  
     on the interval `[1 numel(A)]`. Use this as the linear index  
     of a seed for your clump.  

while clump_size < desired_clump_size     
    2) Generate a list of all positions in the matrix adjacent to  
         (but not already included in) the existing clump.  
    3) Randomly select one of these indices  
    4) Grow the clump by placing an element in this position.  
end 

我不打算写代码;实现起来应该不难,特别是当这段代码不是您整个项目的性能瓶颈时。

编辑:由于您自己尝试过,这里有一些代码:

desired_clump = 5;
matrix_size = 5;
A = zeros(matrix_size);
[C,R]=meshgrid(1:size(A,1), (1:size(A,2))'); %'# row and column numbers for each element

seed = ceil(rand(1)*numel(A));
#% I would have used randi(1) but octave online utility doesn't have it
A(seed) = 1; #% initialize a clump
clump_size = 1;

while clump_size < desired_clump
    CI = A==1; #% logical index of current clump
    CR = reshape(R(CI),1,1,[]); #% 1x1xN index of row values of current clump
    CC = reshape(C(CI),1,1,[]); #% 1x1xN index of col values of current clump
    ADJ = sum(bsxfun(@(x,y)abs(x-y),R,CR)<=1 & bsxfun(@(x,y)abs(x-y),C,CC)<=1, 3)>0 & ~A;
    #% ADJ is the indices of the elements adjacent to the current clump
    B=A; #% for display purposes only
    B(ADJ)=2;
    disp(B)
    disp(' ')
    POS = find(ADJ); #% linear indices of the adjacent elements
    IND = ceil(rand(1)*numel(POS)); #% random index into POS vector
    A(POS(IND))=1; #% grow the clump
    clump_size = clump_size+1;
end
disp(A);

输出: 1表示簇中的元素; 2 表示有资格进行团 block 扩展

iteration 1:
   0   0   2   1   2
   0   0   2   2   2
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0
iteration 2: 
   0   0   2   1   2
   0   0   2   1   2
   0   0   2   2   2
   0   0   0   0   0
   0   0   0   0   0
iteration 3: 
   0   0   2   1   2
   0   0   2   1   2
   0   0   2   1   2
   0   0   2   2   2
   0   0   0   0   0
iteration 4: 
   0   0   2   1   1
   0   0   2   1   2
   0   0   2   1   2
   0   0   2   2   2
   0   0   0   0   0

Final clump:
   0   0   0   1   1
   0   0   1   1   0
   0   0   0   1   0
   0   0   0   0   0
   0   0   0   0   0

每次生成一个随机数应该不会那么慢。如果它确实是一个瓶颈,毫无疑问还有一些方法可以加快它的速度。希望这个例子能让你更进一步。

关于matlab - 用 block 状或聚合的方法随机替换矩阵中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655769/

相关文章:

c# - .Net 的 `Random` 类中的错误?

java - Java 中的随机标准化

java - 如何创建一个在给定范围内随机打乱数字的 int 数组

java - 通过矩阵的最小成本

python - Python 中 Matlab 'fscanf' 的等价物是什么?

matlab - 计算信号的 SNR

matlab - 通过减小最大向量的大小,使不等长向量的长度相等

matlab - 查找 NaN 值是元胞数组

c++ - 如何在 C++ 中使用 <threads> 和一维数组进行矩阵乘法?

internet-explorer-8 - dximagetransform.matrix,在 IE 8 标准模式下绝对定位不旋转的子元素