Question

In R, I can construct a matrix of random sample by

> replicate(10, sample(1:100,2))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   93   37   62   76   82   22   11   16   72    34
[2,]   95   21   13   48   59   49   38  100   90    27

Each column represents a pair of random sample from 1:100. I wonder if there is any Julia equivalence? I have tried the following

julia> [sample(1:100,2,replace=false) for i in 1:10]
10-element Array{Array{T,1},1}:
 [96,53]
 [3,31] 
 [14,23]
 [21,46]
 [78,76]
 [58,64]
 [35,85]
 [95,99]
 [88,42]
 [93,31]

But it is array of array, not quite what I want.

Was it helpful?

Solution

I don't know if this is the best way, but you can modify your example with hcat to get a matrix:

hcat([sample(1:100, 2) for i = 1:10]...)

OTHER TIPS

You can use a two-dimensional comprehension:

[sample(1:100) for i in 1:2, j in 1:10]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top