Skip to content

sample #404

Open
Open
sample#404
@Beliavsky

Description

@Beliavsky

A sample function, similar to that of R, would be useful.

y = sample(x,n,replace=.true.)

for array x(:) would return an array y(:) with n elements from x. If n > size(x) and replace=.FALSE. there would be a run-time error.

Sample could be usefully generalized to N-dimensional arrays, with the user specifying the dimension along which to sample. For matrix x(:,:) with shape (n1,n2)

y = sample(x,n,dim=dim)

would return an array of shape (n,n2) if dim = 1 (sampling rows) or (n1,n) if dim = 2 (sampling columns).

Generating a sample is simple once you have generated the corresponding vector subscript. I believe random integers are going to be part of stdlib. The case of sampling integers from 1 to N without replacement would be needed. You could use a function that generates a vector of random integers to sample an array of derived types on the fly.

A common special case of sample would be permute -- return an array of the same shape but shuffled along a dimension. A user could want many permutations of a 1D array, so
for an array x(:) with length m

permute(x,ncopies)

could return a matrix with shape (m,ncopies). In general permute(x,ncopies) would result in an array with one rank higher, with the extra dimension appended. One could allow ncopies as an argument for sample. It would yield an array 1 rank higher than sample without the ncopies argument.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: algorithmssearching and sorting, merging, ...

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions