Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer depends on whether you store the columns or the rows of matrix. Assuming you have a <code>n * n</code> size matrix and you store the rows, so </p> <pre><code>A = [[1,2,3,4], [2,3,4,5], [1,2,3,4], [3,2,1,4]] </code></pre> <p>and the starting point is <code>i</code>, you should go from the array no. <code>m = i div n</code> (the integer part of the division, rounding down), and inside the array, the staring element should be the no. <code>p = i mod n</code> (the modulus). And from that point, you can select every array from <code>m</code> to <code>n</code>, and in every array, the <code>p</code>th element, until the most recent element is the same as your original.</p> <p>In Java-like code:</p> <pre><code>int n = 4; int[][] A = new int[n][n]; ... // initialize A int sumValues(int i) { int original = A[i/n][i%n]; int sum = original; int p = i % n; for (m = i/n + 1, m&lt;n, ++m) { if (A[m][p] != orginal) sum += A[m][p]; else break; } return sum; } </code></pre> <p>If you are storing the columns, so</p> <pre><code>A = [[1,2,1,3], [2,3,2,2], [3,4,3,1], [4,5,4,4]] </code></pre> <p>then <code>m</code> and <code>p</code> are inversed, meaning that from <code>A</code> you should select array no. <code>m = i mod n</code> and inside that array, element no. <code>p = i div n</code>. After this, you stay in your selected array, and just increase <code>p</code> until <code>A[m][p]</code> equals to the originally selected value.</p> <p>In Java-like code:</p> <pre><code>int n = 4; int[][] A = new int[n][n]; ... // initialize A int sumValues(int i) { int original = A[i%n][i/n]; int sum = original; int p = i / n; for (p = i/n +1, p&lt;n, ++p) { if (A[m][p] != orginal) sum += A[m][p]; else break; } return sum; } </code></pre> <p>But correct me, if I'm wrong :)</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. This table or related slice is empty.
    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