Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand amit's question, what you can do in Matlab is the following:</p> <p>Data:</p> <pre><code>M = 4e3; % M different cases N = 5e2; % N sources K = 5e1; % K targets A = rand(M, N); % M-by-N matrix of random numbers A = A ./ repmat(sum(A, 2), 1, N); % M-by-N matrix of probabilities (?) B = rand(N, K); % N-by-K matrix of random numbers B = B ./ repmat(sum(B), N, 1); % N-by-K matrix of probabilities (?) </code></pre> <p>First solution</p> <pre><code>% One-liner solution: tic C = squeeze(1 - prod(1 - repmat(A, [1 1 K]) .* permute(repmat(B, [1 1 M]), [3 1 2]), 2)); toc % Elapsed time is 6.695364 seconds. </code></pre> <p>Second solution</p> <pre><code>% Partial vectorization 1 tic D = zeros(M, K); for hh = 1:M tmp = repmat(A(hh, :)', 1, K); D(hh, :) = 1 - prod((1 - tmp .* B), 1); end toc % Elapsed time is 0.686487 seconds. </code></pre> <p>Third solution</p> <pre><code>% Partial vectorization 2 tic E = zeros(M, K); for hh = 1:M for ii = 1:K E(hh, ii) = 1 - prod(1 - A(hh, :)' .* B(:, ii)); end end toc % Elapsed time is 2.003891 seconds. </code></pre> <p>Fourth solution</p> <pre><code>% No vectorization at all tic F = ones(M, K); for hh = 1:M for ii = 1:K for jj = 1:N F(hh, ii) = F(hh, ii) * prod(1 - A(hh, jj) .* B(jj, ii)); end F(hh, ii) = 1 - F(hh, ii); end end toc % Elapsed time is 19.201042 seconds. </code></pre> <p>The solutions are equivalent …</p> <pre><code>chck1 = C - D; chck2 = C - E; chck3 = C - F; figure plot(sort(chck1(:))) figure plot(sort(chck2(:))) figure plot(sort(chck3(:))) </code></pre> <p>… but apparently the approaches with partial vectorization, without repmat and permute, are more efficient in terms of memory and execution time.</p>
 

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