If you don't care about performance, the generalization of your method is 
`mapslices`.

If you do care about performance and you're running Julia 0.4,
check out the Cartesian iterators:
http://docs.julialang.org/en/latest/manual/arrays/#iteration

Something like:

    for I in CartesianRange(Istart, Iend)
        Acurrent = A[I]
        for d = 1:N
            counter[d] += Acurrent >= A[I-offset[d]]
        end
    end

where offset[d] is a vector of CartesianIndexes that look like (0,0,1,0,0,...)
(1 is in the dth slot).

Best,
--Tim


On Tuesday, August 11, 2015 09:50:07 AM Nils Gudat wrote:
> This is a re-post of sorts of a question I already asked a year ago in the
> user group. At the time, Stefan Karpinski proposed that base.issorted might
> be expanded to accomodate checking in multiple dimensions, but looking at
> the docs it seems this hasn't happened. This post now has two purposes:
> 1. To ask if anyone knows of such a functionality, maybe in a package?
> 2. To see if someone has an idea about how to improve my approach. I'm
> basically doing this:
> 
> function checkmonotonicity(v::Array{Float64,2})
>   counter = zeros(2)
> 
>   for b = 1:size(v,2)
>     counter[1] += ~issorted(v[:, b])
>   end
> 
>   for a = 1:size(v,1)
>     counter[2] += ~issorted(v[a, :])
>   end
> 
>   return counter
> end
> 
> which for three dimensions requires me to write:
> 
> function checkmonotonicity(v::Array{Float64,3})
>   counter = zeros(3)
> 
>   for c = 1:size(v,3), b = 1:size(v,2)
>     counter[1] += ~issorted(v[:, b, c])
>   end
> 
>   for c = 1:size(v,3), a = 1:size(v,1)
>     counter[2] += ~issorted(v[a, :, c])
>   end
> 
>   for b = 1:size(v,2), a = 1:size(v,1)
>     counter[3] += ~issorted(v[a, b, :])
>   end
> 
>   return counter
> end
> 
> and so on for higher dimensions. Obviously, this quickly gets cumbersome
> for higher dimensions, so I wanted to see if anyone could recommend a smart
> way of doing this (maybe via metaprogramming?).
> 
> (For context, I'm actually interested in the number of monotonicity
> violations, which is why I count them rather than just breaking at the
> first violation in a given dimension. This isn't too important, so if
> there's an existing function that simply returns a Boolean for each
> dimension, I'm happy with that.)

Reply via email to