Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, the code below will improve the timing significantly. It's not perfect yet, it can all be optimized a lot further. </p> <p>But, before I do so: I think what you want is fundamentally impossible. </p> <p>So you want </p> <ul> <li>all rows contain the numbers 1 through 12, in a random permutation</li> <li>any value between 1 and 4 must be present either twice or not at all in any column</li> </ul> <p>I have a hunch this is <strong>impossible</strong> (that's why your code <em>never</em> completes), but let me think about this a bit more. </p> <p>Anyway, my 5-minute-and-obvious-improvements-only-version: </p> <pre><code>clc clear all jum_kel = 8; jum_bag = 12; uk_pop = 10; A = jum_kel; % renamed to make language independent B = jum_bag; % and a lot shorter for readability C = uk_pop; krom = zeros(A, B, C); for ii = 1:C; for a = 1:A krom(a,:,ii) = randperm(B); end end gab1 = sum(krom == 1); gab2 = sum(krom == 2); gab3 = sum(krom == 3); gab4 = sum(krom == 4); gabh1 = sum( gab1 ~= 2 &amp; gab1 ~= 0 ); gabh2 = sum( gab2 ~= 2 &amp; gab2 ~= 0 ); gabh3 = sum( gab3 ~= 2 &amp; gab3 ~= 0 ); gabh4 = sum( gab4 ~= 2 &amp; gab4 ~= 0 ); tot = gabh1+gabh2+gabh3+gabh4; for ii = 1:C ii while tot(:,:,ii) ~= 0 for a = 1:A krom(a,:,ii) = randperm(B); end gabb1 = sum(krom(:,:,ii) == 1); gabb2 = sum(krom(:,:,ii) == 2); gabb3 = sum(krom(:,:,ii) == 3); gabb4 = sum(krom(:,:,ii) == 4); gabbh1 = sum(gabb1 ~= 2 &amp; gabb1 ~= 0) gabbh2 = sum(gabb2 ~= 2 &amp; gabb2 ~= 0); gabbh3 = sum(gabb3 ~= 2 &amp; gabb3 ~= 0); gabbh4 = sum(gabb4 ~= 2 &amp; gabb4 ~= 0); tot(:,:,ii) = gabbh1+gabbh2+gabbh3+gabbh4; end end </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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