# Permutedims does not reassign inputted matrix

**URL:** <https://discourse.julialang.org/t/permutedims-does-not-reassign-inputted-matrix/23534>\
**Category:** New to Julia\
**Created:** [April 25, 2019, 10:12pm UTC](https://discourse.julialang.org/t/permutedims-does-not-reassign-inputted-matrix/23534 "2019-04-25T22:12:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![congvan](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@congvan](https://discourse.julialang.org/u/congvan)\
**Post date:** [April 25, 2019, 10:12pm UTC](https://discourse.julialang.org/t/permutedims-does-not-reassign-inputted-matrix/23534/1 "2019-04-25T22:12:12Z")

</div>

I have the following codes:

```julia
julia> A = [1 2; 3 4];

julia> function test!(A)
           A = permutedims(A, [2,1])
       end
test! (generic function with 1 method)

julia> test!(A)
2×2 Array{Int64,2}:
 1 3
 2 4

julia> A
2×2 Array{Int64,2}:
 1 2
 3 4

```

Could someone explain why I reassign A inside test!, but A is not changed? Thanks!

---

<div class="post-metadata">

**Author:** ![congvan](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@congvan](https://discourse.julialang.org/u/congvan)\
**Post date:** [April 25, 2019, 11:00pm UTC](https://discourse.julialang.org/t/permutedims-does-not-reassign-inputted-matrix/23534/2 "2019-04-25T23:00:31Z")

</div>

Never mind, I have answer to my own question 😃 (I can change the values but cannot change the pointer and permutedims create a new local variable). I have another related question. I have the following codes:

```julia
A = [1 2; 3 4]
b = [5.0 7; 6 8]

function solve!(A,b)
    sol = A\b
    b .= sol
end

function solveTwoTime!(A,b)
    solve!(A,b)
    b = permutedims(b,[2,1]) # permute rows vs columns
    solve!(A,b)
    b = permutedims(b,[2,1])
end

```

The idea is I want to call solve two times, first with the columns, then with the rows. However, I do not want to create new local variable. If permutedims can manipulate b (like reshape) and does not create new local variable, then it will be great! Is there anyway to handle this case? Basically the idea is I do not want to create a local variable to improve the performance. Thanks.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 26, 2019, 4:50am UTC](https://discourse.julialang.org/t/permutedims-does-not-reassign-inputted-matrix/23534/3 "2019-04-26T04:50:26Z")

</div>

Just do `b'` which is a lazy transpose. Also, consider calling `factorize(A) ` and use the returned object to solve twice.

Also see

```julia
julia> using LinearAlgebra

help?> LinearAlgebra.ldiv!
  ldiv!(Y, A, B) -> Y

  Compute A \ B in-place and store the result in Y, returning the result.

  The argument A should not be a matrix. Rather, instead of matrices it should be
  a factorization object (e.g. produced by factorize or cholesky). The reason for
  this is that factorization itself is both expensive and typically allocates
  memory (although it can also be done in-place via, e.g., lu!), and
  performance-critical situations requiring ldiv! usually also require
  fine-grained control over the factorization of A.

```
