# [ncl-talk] generate_sample_indices without replacement

Rashed Mahmood rashidcomsis at gmail.com
Fri Feb 18 09:03:05 MST 2022

```Assuming that you need ensemble means of the 40 randomly selected members
for 1000 times, then something as following should work:

nper   = 1000  ; number of times that you want to select 40 random
members
new_arr   = new((/nper,dimx(0),dimx(2),dimx(3)/),typeof(T2m),T2m@
_FillValue)
new_arr!0 = "randomEM"
new_arr!1 = T2m!0
new_arr!2 = T2m!2
new_arr!3 = T2m!3
new_arr&\$new_arr!0\$ = ispan(0,nper-1,1)
new_arr&\$new_arr!1\$ = T2m&\$T2m!0\$
new_arr&\$new_arr!2\$ = T2m&\$T2m!2\$
new_arr&\$new_arr!3\$ = T2m&\$T2m!3\$
new_arr&\$new_arr!2\$@units = T2m&\$T2m!2\$@units
new_arr&\$new_arr!3\$@units = T2m&\$T2m!3\$@units
new_arr at units = T2m at units

do j= 0,nper-1
iwo = generate_sample_indices( nens, 0 ) ; generate sample ids of
size "nens"
new_arr(j,:,:,:) = dim_avg_n_Wrap(T2m(:,iwo(0:n-1),:,:),1)
; get average of the n=40 members
end do

hope that help!...
Rashed

On Fri, Feb 18, 2022 at 2:21 AM Sri nandini via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Thank you!
>
> Would some one help me understand how to modify this if i need to
> subsample multiple times?
>
> Sri
> On 17.02.22 19:10, Rashed Mahmood wrote:
>
> Hi Sri,
> I am not fully sure what your end goal is here. However, if you need to
> randomly select 40 members only once then you do not need a loop for it.
> Also, if you want to select from 100 members then the function
> "generate_sample_indices" should have n=100 instead of 40 that you used.
>
> So if you are interested in randomly selecting *40 members ONLY ONCE *then
> Note that I turned OFF the loop because it is not needed for this example.
> You can use a loop if you need to randomly select 40 members many times.
>
> ;   do j= 0,n-1
>      iwo = generate_sample_indices( nens, 0 )
>      rcp_40= T2m(:,iwo(0:n-1),:,:)
>
> ;   end do
>
>
> On Thu, Feb 17, 2022 at 8:28 AM Sri nandini via ncl-talk <
> ncl-talk at mailman.ucar.edu> wrote:
>
>> Hello dear ncl-users,
>>
>> I would like to know how to properly code for selecting 40 random
>> ensemble from a 100 ensembles of my data.
>>
>> I wonder if someone could help me with this code? My original data is
>> [time | 240] x [ens | 100] x [lat | 180] x [lon | 360]
>>
>>    f     = addfile ("temperature_data.nc", "r")
>>    T2m    = f->temp
>>
>>    printVarSummary(T2m)
>>    dimx = dimsizes(T2m)
>>    ntim = dimx(0)          ; 240- time dimension
>>    nens = dimx(1)          ; 100- ensemble dimension
>>    nlat = dimx(2)            ; 180- lat
>>    mlon = dimx(3)          ; 360- lon
>>
>>    T2m at _FillValue = -9.96921e+36
>>
>>    ; randomly select 40 members from 100 members without replacement
>>    ; define a new variable dimensions
>>
>>    n=40
>>    rcp_40=new((/ntim,n,nlat,mlon/),typeof(rcp45_anom))
>>    printVarSummary(rcp_40) ==>Dimensions and sizes:    [time | 240] x
>> [40] x [lat | 180] x [lon | 360]
>>
>>    Coordinates:
>>              time: [1192152..1366728]
>>              lat: [-89.5..89.5]
>>              lon: [   0.. 359]
>>
>>    do j= 0,n-1
>>    iwo = generate_sample_indices( n, 0 )
>>    rcp_40(:,j,:,:) = T2m(:,iwo,:,:)  ====>error here fatal:VarVarWrite:
>> Number of dimensions on left hand side does not match right hand side
>>
>>    end do
>>    printVarSummary(rcp_40)
>>
>> Could someone help me in this code?
>>
>> Best
>>
>> Sri
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at mailman.ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
> --
> Dr. Sri Nandini-Weiss
>
> Center for Earth System Research and Sustainability (CEN)
> Cluster of Excellence 'Climate, Climatic Change, and Society' (CLICCS)
>
> Universität Hamburg
> Institute of Oceanography
> Bundesstraße 53
> 20146 Hamburg
> Germany
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at mailman.ucar.edu
> List instructions, subscriber options, unsubscribe:
> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220218/be9204ac/attachment.html>
```