Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a way to compute it manually, assuming the set of n vectors is stored in a matrix <code>A</code>, row by row:</p> <ol> <li><p>Obtain all possible pairs of indices (see <a href="https://stackoverflow.com/questions/7446946/how-to-generate-all-pairs-from-two-vectors-in-matlab-using-vectorised-code">this question</a> for possible answers). For instance:</p> <pre><code>[idx2, idx1] = find(ones(N, N)); </code></pre> <p>The corresponding pairs are given by:</p> <pre><code>pairs = [idx1(:), idx2(:)]; </code></pre> <p>Alternatively, if you're not interested in repetitions (<em>e.g.</em> you don't want the sum A<sub>1</sub>+A<sub>1</sub>, etc.), you can use <code>nchoosek</code>:</p> <pre><code>pairs = nchoosek(1:N, 2) idx1 = pairs(:, 1); idx2 = pairs(:, 2); </code></pre></li> <li><p>Use each pair of indices to sum the corresponding rows in <code>A</code>:</p> <pre><code>sums = A(idx1(:), :) + A(idx2(:), :); </code></pre> <p>Alternatively, if you want the total sum of elements for each pair of A<sub>i</sub> and A<sub>j</sub>, you can do <code>sum(A(idx1(:), :) + A(idx2(:), :), 2)</code> instead.</p></li> </ol> <h3>Example</h3> <p>Here's an example for <code>N = 3</code>:</p> <pre><code>A = [1 2 3; 2 3 4; 3 4 5]; N = size(A, 1); [idx2, idx1] = find(ones(N, N)); pairs = [idx1(:), idx2(:)]; sums = A(idx1(:), :) + A(idx2(:), :); </code></pre> <p>The result is:</p> <pre><code>pairs = 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 sums = 2 4 6 3 5 7 4 6 8 3 5 7 4 6 8 5 7 9 4 6 8 5 7 9 6 8 10 </code></pre>
    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. 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.
    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