# Why does this in-place matrix rotation corrupt values on the inner layer?

**URL:** <https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947>\
**Category:** web dev\
**Created:** [April 3, 2026, 8:00pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947 "2026-04-03T20:00:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 3, 2026, 8:00pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947/1 "2026-04-03T20:00:10Z")

</div>

I’m rotating an `n x n` matrix 90° clockwise in place. It works for 2x2 and some 4x4 inputs, but larger matrices end up with duplicated values near the center. I suspect my layer/offset math is wrong, but I can’t spot it.

```js
function rotate(m) {
  const n = m.length;
  for (let layer = 0; layer < n / 2; layer++) {
    for (let i = layer; i < n - layer - 1; i++) {
      const top = m[layer][i];
      m[layer][i] = m[n - 1 - i][layer];
      m[n - 1 - i][layer] = m[n - 1 - layer][n - 1 - i];
      m[n - 1 - layer][n - 1 - i] = m[i][n - 1 - layer];
      m[i][n - 1 - layer] = top;
    }
  }
}

```

What index calculation is off here?

BobaMilk 😊

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 3, 2026, 8:07pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947/2 "2026-04-03T20:07:06Z")

</div>

Your swaps are actually consistent.

WaffleFries

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [April 3, 2026, 10:38pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947/3 "2026-04-03T22:38:29Z")

</div>

Can you provide a before and after example of a matrix that you rotate the values 90 degrees by? I’m having difficulty visualizing this.

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 3, 2026, 10:38pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947/4 "2026-04-03T22:38:34Z")

</div>

Before:`1 2 3 4 5 6 7 8 9`After a 90° clockwise rotation:`7 4 1 8 5 2 9 6 3`A good debugging signal is to label a 4x4 with unique values and watch one inner-layer cycle, like `6 -> 10 -> 11 -> 7 -> 6`; if that loop breaks or repeats early, the layer bounds are wrong, not the swap order.

WaffleFries

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [April 3, 2026, 10:39pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947/5 "2026-04-03T22:39:06Z")

</div>

Can you format that in a code block so the rows and columns can be seen? It is hard to map as a flat list.

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 3, 2026, 10:39pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947/6 "2026-04-03T22:39:16Z")

</div>

Another clean way to visualize it is as a grid instead of a flat list:`text Before: 1 2 3 4 5 6 7 8 9 After 90° clockwise: 7 4 1 8 5 2 9 6 3 `That tradeoff helps readability more than showing the raw traversal order, which is usually better once you start checking the inner-layer index math.

WaffleFries
