# Translate matrix 3x3 vs 4x4

**URL:** <https://forums.swift.org/t/translate-matrix-3x3-vs-4x4/40724>\
**Category:** Using Swift\
**Tags:** simd\
**Created:** [September 28, 2020, 3:53pm UTC](https://forums.swift.org/t/translate-matrix-3x3-vs-4x4/40724 "2020-09-28T15:53:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexSedykh](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AlexSedykh](https://forums.swift.org/u/AlexSedykh)\
**Post date:** [September 28, 2020, 3:53pm UTC](https://forums.swift.org/t/translate-matrix-3x3-vs-4x4/40724/1 "2020-09-28T15:53:19Z")

</div>

Why, if the dimension of matrices is 3x3 for using a translate matrix, the correct result is obtained when we multiply a vector by a matrix, and when using 4x4 matrices, the correct result is obtained when we multiply a matrix by a vector?

```
func check() {
    
    let delta: SIMD2<Float> = [100, 100]
    
    var matrix3x3 = matrix_identity_float3x3
    matrix3x3[0, 2] = delta.x
    matrix3x3[1, 2] = delta.y
    
    var matrix4x4 = matrix_identity_float4x4
    matrix4x4[3, 0] = delta.x
    matrix4x4[3, 1] = delta.y
    
    let dote3: SIMD3<Float> = [10, 10, 1]
    let dote4: SIMD4<Float> = [10, 10, 0, 1]
    
    let r1 = matrix3x3 * dote3 // SIMD3<Float>(10.0, 10.0, 2001.0) // bad
    let r2 = dote3 * matrix3x3 // SIMD3<Float>(110.0, 110.0, 1.0) // good
    let r3 = matrix4x4 * dote4 // SIMD4<Float>(110.0, 110.0, 0.0, 1.0) // good
    let r4 = dote4 * matrix4x4 // SIMD4<Float>(10.0, 10.0, 0.0, 2001.0) // bad

    
}

```

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [September 28, 2020, 4:07pm UTC](https://forums.swift.org/t/translate-matrix-3x3-vs-4x4/40724/2 "2020-09-28T16:07:31Z")

</div>

Your 3x3 matrix is:

```swift
 1 0 0
 0 1 0
100 100 1

```

Your 4x4 matrix is:

```swift
 1 0 0 100
 0 1 0 100
 0 0 1 0
 0 0 0 1

```

Does this help explain what you're seeing?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [September 28, 2020, 4:08pm UTC](https://forums.swift.org/t/translate-matrix-3x3-vs-4x4/40724/3 "2020-09-28T16:08:45Z")

</div>

_Correct_ is quite a needlessly strong word and doesn't tell us much since we don't know what exactly you're confusing about. Your confusion might be from one of these:

- You're treating the vector as a row vector when doing `vector * matrix`, and treating it as a column vector when doing `matrix * vector` (the multiplication wouldn't work otherwise).
- The subscript is in a column-row pair, so `matrix[column, row]`.

---

<div class="post-metadata">

**Author:** ![AlexSedykh](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AlexSedykh](https://forums.swift.org/u/AlexSedykh)\
**Post date:** [September 28, 2020, 4:14pm UTC](https://forums.swift.org/t/translate-matrix-3x3-vs-4x4/40724/4 "2020-09-28T16:14:13Z")

</div>

Thanks a lot. I got the matrices from two different websites.
