Re: vector subscripting

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Mon Mar 29 2010 - 10:19:02 MDT

Hi Tim,

If you have enough memory to convert data_arr to a 1D array, then you
could
convert the lat_idx, lon_idx arrays into a 1D array that contains
indexes
into a big 1D array, and subscript the 1D version of "data_arr" with
this.

Here's an example:

; Generate a dummy nlat x nlon array
    nlat = 4
    nlon = 6
    data_arr = onedtond(ispan(1,nlat*nlon,1),(/nlat,nlon/))

; Print original array
    format = nlon + "i4"
    write_matrix(data_arr,format,0)

; Generate some dummy indexes
    lat_idx = (/ (/1,2,0/), (/3,0,2/) /)
    lon_idx = (/ (/3,2,1/), (/2,2,0/) /)

; Get the size of what will be our new array
    dims = dimsizes(lat_idx)
    N = dims(0)
    M = dims(1)

; Convert the 2D indexes to indexes into a big 1D array.
    latlon_idx_1d = ndtooned(lat_idx)*nlon + ndtooned(lon_idx)

; Convert data_arr to 1D so we can subscript it with 1D indexes.
    data_arr_1d = ndtooned(data_arr)

; Here's the new array.
    result = onedtond(data_arr_1d(latlon_idx_1d),(/N,M/))

; Clean up
   delete(data_arr_1d)
   delete(lalon_idx_1d)

; Print new array
    format = M + "i4"
    write_matrix(result,format,0)

--Mary

On Mar 29, 2010, at 1:01 AM, Timothy W. Hilton wrote:

> Hello NCL-talk,
>
> I have three 2D arrays: data_arr, lat_idx, and lon_idx. lat_idx and
> lon_idx (both NxM) contain coordinate subscripts for data_arr; I need
> the NxM array where
> result(i,j) = data_arr( {lat_idx(i,j)}, {lon_idx(i,j)} )
>
> It seems to me that vector subscripting does not work here, because
> data_arr( ndtooned(lat_idx), ndtooned(lon_idx) )
> results in an array with (N*M)^2 * (N*M)^2 elements, not N*M * N*M
> elements.
>
> I can do the job with nested loops, but it is too slow to be useful
> for
> the amount of data I have. Can anyone suggest a more efficient way
> using subscripting and/or built-in functions?
>
> Any help greatly appreciated!
>
> best,
> Tim
>
> --
>
> Timothy W. Hilton
> PhD Candidate, Department of Meteorology
> The Pennsylvania State University
> 503 Walker Building, University Park, PA 16802
> hilton@meteo.psu.edu
>
>
>
>
>
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Mar 29 10:20:16 2010

This archive was generated by hypermail 2.1.8 : Thu Apr 01 2010 - 11:31:45 MDT