Matrix Initialization with Lamba Expression Missing Dimensions

a=[1 2 3]'
b1=[a a.^2 a.^3] #withour lambda
b2=[a.^i for i in 1:3] #with lambda
@show b1,size(b1)
@show b2,size(b2)
b1 = [1 1 1; 2 4 8; 3 9 27]
b2 = [[1; 2; 3;;], [1; 4; 9;;], [1; 8; 27;;]]
(size(b1), size(b2)) = ((3, 3), (3,))

Why b2 has shape (3,)? Seems the dimension except the first one is missing. What’s the right way to use lambda expression to initialize a matrix from vectors?

What you could do loop over all values of a while looping the power. So now it takes the first value of a and then to the power 1:3, then it takes the second value of a etc.

b2[a[i].^j for i in 1:3, j in 1:3]

And if you want to generalize it even more:

a=[1 2 3 4 5]'
lambda = 5
b2[a[i].^j for i in 1:size(a,1), j in 1:lambda]

Because it is a Vector of Arrays. To elaborate, [f(i) for i in 1:N] is an array comprehension and will result in a 1D Array (aka a Vector) of length N (or size (N,)). The element type of the Vector will be determined by the output of f(i). So in your case, you end up with a Vector of Matrixes.

One way is

b2 = reduce(hcat, (a.^i for i in 1:3))