# Help me extend this solution for high dimensional matrices in Julia

**URL:** <https://discourse.julialang.org/t/help-me-extend-this-solution-for-high-dimensional-matrices-in-julia/40459>\
**Category:** New to Julia\
**Tags:** matrices\
**Created:** [May 30, 2020, 5:40am UTC](https://discourse.julialang.org/t/help-me-extend-this-solution-for-high-dimensional-matrices-in-julia/40459 "2020-05-30T05:40:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yourboygorja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yourboygorja/32/15305_2.png) [@yourboygorja](https://discourse.julialang.org/u/yourboygorja)\
**Post date:** [May 30, 2020, 5:40am UTC](https://discourse.julialang.org/t/help-me-extend-this-solution-for-high-dimensional-matrices-in-julia/40459/1 "2020-05-30T05:40:26Z")

</div>

[https://stackoverflow.com/questions/62097898/extend-this-solution-for-high-dimensional-matrices-in-julia](https://stackoverflow.com/questions/62097898/extend-this-solution-for-high-dimensional-matrices-in-julia)

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [May 30, 2020, 8:06am UTC](https://discourse.julialang.org/t/help-me-extend-this-solution-for-high-dimensional-matrices-in-julia/40459/2 "2020-05-30T08:06:41Z")

</div>

Seems like the only reason it wouldn’t extend as written is the matrix transposes. If you know the behavior you want in higher dimensions, this should be simple enough. E.g. if always the first two dimensions should be swapped you could do the same thing with `permutedims`. Otherwise I think all those operations are legal on arbitrary arrays.

---

<div class="post-metadata">

**Author:** ![yourboygorja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yourboygorja/32/15305_2.png) [@yourboygorja](https://discourse.julialang.org/u/yourboygorja)\
**Post date:** [May 30, 2020, 8:27am UTC](https://discourse.julialang.org/t/help-me-extend-this-solution-for-high-dimensional-matrices-in-julia/40459/3 "2020-05-30T08:27:54Z")

</div>

I agree. Its because of the transpose. Can you please help me in it though. Its my second day at Julia and I am porting a code from Python to Julia looking for speed benefits.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [May 30, 2020, 8:36am UTC](https://discourse.julialang.org/t/help-me-extend-this-solution-for-high-dimensional-matrices-in-julia/40459/4 "2020-05-30T08:36:51Z")

</div>

What should be the behavior for higher dimensional arrays? My suggestion is to forget that you want a one-liner, and figure out what should happen in a 3D case (since right now that isn’t super well defined). Maybe do it in a for-loop with an `if Matrix2[i, j] == 1` check to start out with, and then go back to the vector indexing once that’s sorted out?
