0

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 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

indices doesn't seem to work anymore. I used rand(axes(b, 1), 10).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.