Converting Matlab max to findmax

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.

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).

4 Likes