Re: dim_minind_n / dim_maxind_n

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Fri Jul 11 2014 - 11:27:44 MDT

Jatin,

I've often wanted a dim_minind type of function myself. I've created a
ticket for this (NCL-1994).

I'm assuming you just want the index into that leftmost dimension only?

This is a quick-and-dirty function that could be slow for large arrays:

;----------------------------------------------------------------------
; Get min indexes across leftmost index of a 4D array
; The "_0" in the name is to indicate this is for
; leftmost dimension.
;----------------------------------------------------------------------
undef("dim_minind_0")
function dim_minind_0(x)
local i,j,k,dims
begin
  dims = dimsizes(x)
  xmin_ind = new(dims(1:),integer)
  do i=0,dims(1)-1
    do j=0,dims(2)-1
      do k=0,dims(3)-1
        xmin_ind(i,j,k) = minind(x(:,i,j,k))
      end do
    end do
  end do
  return(xmin_ind)
end

;----------------------------------------------------------------------
; Main code
;----------------------------------------------------------------------
begin
  dims = (/4,3,2,3/)
  x = random_uniform(0,100,dims)
  xmin_ind = dim_minind_0(x)

; print out values just for fun
  do i=0,dims(1)-1
    do j=0,dims(2)-1
      do k=0,dims(3)-1
        dim_str = str_join(""+(/i,j,k/),",")
        val_str = str_join(""+x(:,i,j,k),",")
        print("x(:," + dim_str + ") = " + val_str)
        print("index = " + xmin_ind(i,j,k) + ", xmin = " +
x(xmin_ind(i,j,k),i,j,k))
      end do
    end do
  end do
end



On Thu, Jul 10, 2014 at 11:05 PM, Jatin Kala <jatin.kala.jk@gmail.com>
wrote:

> I meant:
> x_min0 = dim_min_n(x,0)
> Not dim_avg_n ....
>
>
>
> On 11/07/14 3:03 PM, Jatin Kala wrote:
> > Hi all,
> > i have an n-dimensional array, lets say x(a,b,c,d). I want to find the
> > min along the first dimension, so i do:
> > x_min0 = dim_avg_n(x,0)
> >
> > x_min0 is size(b,c,d)
> >
> > But i also want an array of same size (b,c,d) as x_min0, which has the
> > indices of the min values.
> >
> > Most languages have a function for this, e.g., numpy.argmax(x,axis=0)
> > for python, and MAXLOC/MINLOC (e.g., MAXLOC(x,dim=1)) in Fortran.
> >
> > There does not seem to be an NCL equivalent????
> > ind_resolve does not do this, as in the case of a 4D array, it returns
> > a 1 by 4 array.
> >
> > It's friday and i might be missing something obvious....
> >
> > Any comments?
> > I also tried to use use WRAPIT by making a F90 subroutine which uses
> > MAXLOC, but it's doing my head in!
> >
> > Cheers,
> > Jatin.
> >
> >
> >
> >
> >
> >
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

Received on Fri Jul 11 05:27:44 2014

This archive was generated by hypermail 2.1.8 : Wed Jul 23 2014 - 15:33:47 MDT