Hmm, I see your point. Looks like we need a "good" version of this. Would you 
please file an issue requesting this feature?

In the meantime, if you need this for only special cases (say, in 3 
dimensions, where you're always taking the max along the 3rd dimension), it 
would only be a handful of lines to write a good version using loops. Or, as 
you did before, use mapslices.

--Tim

On Thursday, May 01, 2014 09:40:09 AM Florian Oswald wrote:
> actually, the ismaxfun is not doing that I want. I want to have the maximum
> value and its index in the given dimension. while the maximum value is
> fine, I can't get the index:
> 
> julia> function ismaxfun(x::Array{Float64},d::Int)
>        z = maximum(x,d)
>        ismax = x .== z
>        (z,ismax)
>        end
> 
> A = rand(2,2,2)
> x = ismaxfun(A,3)
> maxval = squeeze(x[1],3)
> maxind = find(x[2])
> 
> maxind contains the linear indices of the maxima in dimension 3. I want to
> map those indices into an array Z of size(maxval)=(2,2), where the value at
> Z(i,j) tells me what the maximal index into along A[i,j,:] is. I can get
> this
> 
> julia> map(x->ind2sub((2,2,2),x),maxind)
> 4-element Array{(Int64,Int64,Int64),1}:
>  (2,1,1)
>  (1,1,2)
>  (1,2,2)
>  (2,2,2)
> 
> which is close, but really I want that:
> 
> ms=mapslices(findmax,A,3)
> julia> map(x->x[2],ms)
> 2x2x1 Array{Int64,3}:
> [:, :, 1] =
>  2  2
>  1  2
> 
> 
> Any suggestion on how to achieve this using ismaxfun above? thanks.
> 
> On Wednesday, 30 April 2014 15:58:16 UTC+1, Florian Oswald wrote:
> > jeez, you are right! sorry i'm doing my first steps here - forget about
> > the compile step. it IS much faster than 2x. after changing ismaxfun to
> > output something similar to slicefun, it's 10x faster. but maybe my
> > reshaping is not the smartest idea either. anyway, that's pretty good!
> > 
> > julia> function ismaxfun(x::Array{Float64,6})
> > 
> >        z = maximum(x,6)
> >        ismax = x .== z
> >        id = reshape(find(ismax),size(x)[1:5])
> >        end
> > 
> > A = rand(10,10,10,10,10,50)
> > 
> > julia> @time k = slicefun(A)
> > elapsed time: 0.912256696 seconds (213759264 bytes allocated)
> > 
> > julia> @time k = ismaxfun(A)
> > elapsed time: 0.089194917 seconds (3484324 bytes allocated)
> > 
> > On Wednesday, 30 April 2014 15:47:30 UTC+1, Tim Holy wrote:
> >> I time it at 50x faster:
> >> 
> >> julia> @time slicefun(A);
> >> elapsed time: 1.135524683 seconds (181765472 bytes allocated)
> >> 
> >> julia> @time ismaxfun(A);
> >> elapsed time: 0.020300699 seconds (931120 bytes allocated)
> >> 
> >> 
> >> Did you run it a second time? (You don't want to include the JIT timing.)
> >> 
> >> --Tim
> >> 
> >> On Wednesday, April 30, 2014 07:38:07 AM Florian Oswald wrote:
> >> > I see. thanks!
> >> > you are (of course) right with the timing:
> >> > 
> >> > julia> function slicefun(x::Array{Float64,6})
> >> > 
> >> >        z = mapslices(findmax,x,6)
> >> >        end
> >> > 
> >> > julia> function ismaxfun(x::Array{Float64,6})
> >> > 
> >> >        z = maximum(x,6)
> >> >        ismax = x .== z
> >> >        end
> >> > 
> >> > times as follows. more than twice as fast!
> >> > 
> >> > julia> @time k = ismaxfun(A)
> >> > elapsed time: 0.53359244 seconds (14418060 bytes allocated)
> >> > julia> @time k = slicefun(A)
> >> > elapsed time: 1.396534023 seconds (226762376 bytes allocated)
> >> > 
> >> > On Wednesday, 30 April 2014 15:25:25 UTC+1, Tim Holy wrote:
> >> > > There is
> >> > > 
> >> > >     maximum(A, 3)
> >> > > 
> >> > > but I gather you also want the index? If so, mapslices is your best
> >> > > one-liner
> >> > > as of now. But if you're performance-sensitive, you might also look
> >> 
> >> into
> >> 
> >> > > this:
> >> > >     M = maximum(A, dims)
> >> > >     ismax = A .== M
> >> > > 
> >> > > and then find the `true` elements of ismax. I'll bet that's quite a
> >> 
> >> lot
> >> 
> >> > > faster
> >> > > than using mapslices, even though it requires two traversals of the
> >> 
> >> array.
> >> 
> >> > > As
> >> > > a bonus, you'll also learn about ties.
> >> > > 
> >> > > --Tim
> >> > > 
> >> > > On Wednesday, April 30, 2014 04:04:47 AM Florian Oswald wrote:
> >> > > > I looked at the issue open at
> >> > > > 
> >> > > > https://github.com/JuliaLang/julia/issues/3893
> >> > > > 
> >> > > > but couldn't figure out what's the best thing to do. I gather that
> >> 
> >> there
> >> 
> >> > > > will be a
> >> > > > 
> >> > > > findmax(A,dims)
> >> > > > 
> >> > > > in some future version of julia? that's certainly what im looking
> >> 
> >> for
> >> 
> >> > > here.
> >> > > 
> >> > > > in the meantime, is this the best I can do?
> >> > > > 
> >> > > > A = randn(3,3,3)
> >> > > > mapslices(findmax,A,3)
> >> > > > 
> >> > > > cheers

Reply via email to