OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Matrix indexing/flattening

  • Thread starter Thread starter Ecanyte
  • Start date Start date
E

Ecanyte

Guest
I am given a matrix with dimensions dim = nxmx4. I would like to flatten this matrix to a new 1-dimensional matrix with length l = n*m*4 using 3 nested for-loops.

Code:
for (let x = 0; x < n; x++) {
    for (let y = 0; y < m; y++) {
        for (let i = 0; i < 4; i++) {
            let index = ?;
            newMatrix[index] = matrix[x][y][i];
        }
    }
}

Additionally the condition index % 4 == i should always hold.

To be more precise, the values of matrix[x][y] don't matter that much for now (can be arbitrary), as long as the index is increased by one each iteration. For i = 0 -> index = 0, 4, 8, ... should be returned | i = 1 -> index = 1, 5, 9, ... | i = 2 -> index = 2, 6, 10, ... | and i = 3 -> index = 3, 7, 11, ...

I can't come up with the correct indexing right now. Thanks in advance.

<p>I am given a matrix with dimensions <code>dim = nxmx4</code>. I would like to flatten this matrix to a new 1-dimensional matrix with length <code>l = n*m*4</code> using 3 nested for-loops.</p>
<pre><code>for (let x = 0; x < n; x++) {
for (let y = 0; y < m; y++) {
for (let i = 0; i < 4; i++) {
let index = ?;
newMatrix[index] = matrix[x][y];
}
}
}
</code></pre>
<p>Additionally the condition <code>index % 4 == i</code> should always hold.</p>
<p>To be more precise, the values of matrix[x][y] don't matter that much for now (can be arbitrary), as long as the index is increased by one each iteration.
For i = 0 -> index = 0, 4, 8, ... should be returned | i = 1 -> index = 1, 5, 9, ... | i = 2 -> index = 2, 6, 10, ... | and i = 3 -> index = 3, 7, 11, ...</p>
<p>I can't come up with the correct indexing right now. Thanks in advance.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top