0
votes

I can sample from a 1-D array just fine. E.g.

julia> a = [1; 2; 3]
3-element Array{Int64,1}:
 1
 2
 3
julia> sample(a, myweights, 5)
5-element Array{Int64,1}:
 1
 2
 1
 3
 3

I can also take weighted samples:

julia> myweights = weights([0.8, 0.1, 0.1])
StatsBase.WeightVec{Float64,Array{Float64,1}}([0.8,0.1,0.1],1.0)

julia> sample(a, myweights, 5)
5-element Array{Int64,1}:
 2
 1
 1
 1
 1

I'd like to do the same thing for a 2D array, but sampling by row and not by element. E.g. if I have the array

julia> b = [1 1 1; 2 2 2; 3 3 3]
3×3 Array{Int64,2}:
 1  1  1
 2  2  2
 3  3  3

I'd like to be able to take unweighted and weighted samples that give me outputs like

 1  1  1
 2  2  2
 1  1  1
 1  1  1
 3  3  3

How can I do this?

1

1 Answers

5
votes

The simplest solution here is to sample from the indices of the rows, and then use that to index into your matrix:

julia> idxs = sample(axes(b, 1), myweights, 10)
10-element Array{Int64,1}:
 1
 1
 1
 2
 1
 1
 3
 1
 1
 1

julia> b[idxs, :]
10×3 Array{Int64,2}:
 1  1  1
 1  1  1
 1  1  1
 2  2  2
 1  1  1
 1  1  1
 3  3  3
 1  1  1
 1  1  1
 1  1  1