Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to do this, is to move each existing element of the original matrix to its new position, taking care to pick up the value at the destination index first, so that it can also be moved to its new position. For an arbitrary NxM matrix, the destination index of an element at index X can be calculated as:</p> <pre><code>X_new = ((N*X) / (M*N)) + ((N*X) % (M*N)) </code></pre> <p>where the "/" operator represents integer division (the quotient) and the "%" is the modulo operator (the remainder) -- I'm using Python syntax here.</p> <p>The trouble is that you're not guaranteed to traverse all the elements in your matrix if you start at an arbitrary spot. The easiest way to work around this, is to maintain a bitmap of elements that have been moved to their correct positions.</p> <p>Here's some Python code that achieves this:</p> <pre><code>M = 4 N = 3 MN = M*N X = range(0,MN) bitmap = (1&lt;&lt;0) + (1&lt;&lt;(MN-1)) i = 0 while bitmap != ( (1&lt;&lt;MN) - 1): if (bitmap &amp; (1&lt;&lt;i)): i += 1 xin = X[i] i = ((N*i)/MN) + ((N*i) % MN) else: xout = X[i] X[i] = xin bitmap += (1&lt;&lt;i) i = ((N*i)/MN) + ((N*i) % MN) xin = xout print X </code></pre> <p>I've sacrificed some optimisation for clarity here. It is possible to use more complicated algorithms to avoid the bitmap -- have a look at the references in the related <a href="http://en.wikipedia.org/wiki/In-place_matrix_transposition" rel="nofollow">Wikipedia article</a> if you're really serious about saving memory at the cost of computation.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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