# Converting Matlab max to findmax

**URL:** <https://discourse.julialang.org/t/converting-matlab-max-to-findmax/26148>\
**Category:** New to Julia\
**Created:** [July 8, 2019, 7:41pm UTC](https://discourse.julialang.org/t/converting-matlab-max-to-findmax/26148 "2019-07-08T19:41:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clemej/32/7098_2.png) [@clemej](https://discourse.julialang.org/u/clemej)\
**Post date:** [July 8, 2019, 7:41pm UTC](https://discourse.julialang.org/t/converting-matlab-max-to-findmax/26148/1 "2019-07-08T19:41:35Z")

</div>

Hi, I’m converting some Matlab code that I’m not very familiar with to Julia, which I’m also not very familiar with, and I’m tripping over myself.

Assume `X` is a 5 dimensional float64 array of shape 5x6x41x41x18. The following code in Matlab returns '`idxs` which is a 1x6x41x41 array:

```
`[~,idxs] = max(abs(X(1,:,:,:,:)),[],5)`

```

Which, I believe is the slice that contains the maximum value reduced along the 5th dimension.

My attempt to convert this line to Julia is:

```
`_, idxs = findmax(abs.(X[1,:,:,:,:]), dims=5)`

```

However, this returns a 6x41x41x18 array. Clearly not the same, the first dimension is gone and the 5th isn’t reduced.

I must missing something obvious. Any pointers? And thanks.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 8, 2019, 7:50pm UTC](https://discourse.julialang.org/t/converting-matlab-max-to-findmax/26148/2 "2019-07-08T19:50:58Z")

</div>

The difference here isn’t in `findmax`, it’s in the indexing expression. To match Matlab, use `X[1:1,:,:,:,:]` to keep everything as 5-dimensional or just work with it on dimension 4.

Julia drops all dimensions indexed by scalars, whereas Matlab preserves them (either implicitly or explicitly).
