I am coming from numpy, trying tosee if Julia can be faster. Right now I am dealing with a function that generates for me what I thought had to be a matrix, but no, it’s a vector of vector. Fine. How do I make it’s transpose?
I generate the Vector{Vector} from list comprehension tab = [ map( (x) -> d[1] + (d[2]-d[1] )*x , rand(Uniform(0,1),Np) ) for d in domains ]
For Np=2 and a 5D domain domains=( (100,1000), (-1,1), (-1,1), (-1,1), (-1,1) ) I get a
size(tab)=(5,)
I need to make it become a (5,2).
I have tried to use cat, but it’s not the right tool. transpose gives me a 1x5 and I cannot manipulate it to a 2D Matrix (2,5).
Any suggestion? Must be a common roadbloack for someone coming from numpy…
Here’s how you can both simplify this and tweak it to return a matrix:
tab_matrix = stack(rand(Uniform(d[1], d[2]), Np) for d in domains)
In this case, the difference is that, to use hcat, you either need to wrap it in reduce as you did, or splat the argument as follows: hcat(tab...). Meanwhile, stack takes tab as a single argument: stack(tab).
In general, stack takes a single iterator of arrays and stacks them along a new dimension—for example, it can take a vector of matrices and stack them to form a 3D array. hcat takes multiple vectors/matrices and concatenates them along the second (column) dimension. For example, hcat([1 2; 3 4], [5; 6]) == [1 2 5; 3 4 6].
More simply, you can use tab = [rand(Uniform(d[1], d[2])) for d in domains, _ in 1:Np]. Or you can generate it transposed from the beginning by switching the dimensions tab_transposed = [rand(Uniform(d[1], d[2])) for _ in 1:Np, d in domains].
Yes, it seems unnecessary and inefficient to first generate a vector of vectors, only to stack or cat it. Just directly use a 2D array (matrix) comprehension that avoids all intermediates.
Or better yet, re-think your whole approach to avoid creating the matrix in the first place. The biggest habit to unlearn, coming from numpy, is the urge to “vectorize” everything into a sequence of steps that generate a bunch of intermediate arrays from relatively tiny calculations (e.g. adding two vectors). Better to avoid calculating the intermediate arrays, and instead fuse the calculations into a single pass. Loops are fast in Julia. See why vectorized code is not as fast as it could be.