Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My approach would be the following:</p> <pre><code>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 &lt; 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 </code></pre> <p>I'm not going to write the code; it shouldn't be difficult to implement, particularly if this piece of code isn't a performance bottleneck in your overall project.</p> <p>EDIT: Since you gave it a try yourself, here's some code:</p> <pre><code>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 &lt; 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)&lt;=1 &amp; bsxfun(@(x,y)abs(x-y),C,CC)&lt;=1, 3)&gt;0 &amp; ~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); </code></pre> <p>Output: <code>1</code> indicates elements in the clump; <code>2</code> means eligible for clump expansion</p> <pre><code>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 </code></pre> <p>Generating a single random number each time shouldn't be all that slow. There are also undoubtedly ways to speed it up if it is truly a bottleneck. Hopefully this example can get you a little further though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload