Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd say that's a pretty good solution you have so far. </p> <p>There is a few optimizations I see though. Here's my variation:</p> <pre><code>% INITIALIZE NN = 9; L = rand(1,NN-1); while numel(L) ~= NN; L = unique( randi(100,1,NN) ); end % Some bogus constraints constraints = [... L(1) L(2) L(3) L(6) L(3) L(5) L(8) L(4)]; % METHOD 0 (your original method) tic orderings = perms(L); p = size(orderings,1); c = size(constraints,1); toKeep = true(p,1); for perm = 1:p for constr = 1:c idxA = find(orderings(perm,:) == constraints(constr,1)); idxB = find(orderings(perm,:) == constraints(constr,2)); if idxA &gt; idxB toKeep(perm) = false; end end end orderings0 = orderings(toKeep,:); toc % METHOD 1 (your original, plus a few optimizations) tic orderings = perms(L); p = size(orderings,1); c = size(constraints,1); toKeep = true(p,1); for perm = 1:p for constr = 1:c % break on first condition breached if toKeep(perm) % find only *first* entry toKeep(perm) = ... find(orderings(perm,:) == constraints(constr,1), 1) &lt; ... find(orderings(perm,:) == constraints(constr,2), 1); else break end end end orderings1 = orderings(toKeep,:); toc % METHOD 2 tic orderings = perms(L); p = size(orderings,1); c = size(constraints,1); toKeep = true(p,1); for constr = 1:c % break on first condition breached1 if any(toKeep) % Vectorized search for constraint values [i1, j1] = find(orderings == constraints(constr,1)); [i2, j2] = find(orderings == constraints(constr,2)); % sort by rows [i1, j1i] = sort(i1); [i2, j2i] = sort(i2); % Check if columns meet condition toKeep = toKeep &amp; j1(j1i) &lt; j2(j2i); else break end end orderings2 = orderings(toKeep,:); toc % Check for equality all(orderings2(:) == orderings1(:)) </code></pre> <p>Results: </p> <pre><code>Elapsed time is 17.911469 seconds. % your method Elapsed time is 10.477549 seconds. % your method + optimizations Elapsed time is 2.184242 seconds. % vectorized outer loop ans = 1 ans = 1 </code></pre> <p>The whole approach however has one fundamental flaw IMHO; the direct use of <code>perms</code>. This inherently poses a limitation due to memory constraints (<code>NN &lt; 10</code>, as stated in <code>help perms</code>). </p> <p>I have a strong suspicion you can get better performance, both time-wise and memory-wise, when you put together a customized <code>perms</code>. Luckily, <code>perms</code> is not built-in, so you can start by copy-pasting that code into your custom function. </p>
    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.
    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