# Matrix Initialization with Lamba Expression Missing Dimensions

**URL:** <https://discourse.julialang.org/t/matrix-initialization-with-lamba-expression-missing-dimensions/87952>\
**Category:** General Usage\
**Created:** [September 28, 2022, 8:45pm UTC](https://discourse.julialang.org/t/matrix-initialization-with-lamba-expression-missing-dimensions/87952 "2022-09-28T20:45:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bigdio/32/31154_2.png) [@bigdio](https://discourse.julialang.org/u/bigdio)\
**Post date:** [September 28, 2022, 8:45pm UTC](https://discourse.julialang.org/t/matrix-initialization-with-lamba-expression-missing-dimensions/87952/1 "2022-09-28T20:45:00Z")

</div>

```julia
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?

```julia

```

---

<div class="post-metadata">

**Author:** ![JorizovdZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorizovdz/32/43064_2.png) [@JorizovdZ](https://discourse.julialang.org/u/JorizovdZ)\
**Post date:** [September 29, 2022, 9:48am UTC](https://discourse.julialang.org/t/matrix-initialization-with-lamba-expression-missing-dimensions/87952/2 "2022-09-29T09:48:09Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![JorizovdZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jorizovdz/32/43064_2.png) [@JorizovdZ](https://discourse.julialang.org/u/JorizovdZ)\
**Post date:** [September 29, 2022, 9:54am UTC](https://discourse.julialang.org/t/matrix-initialization-with-lamba-expression-missing-dimensions/87952/3 "2022-09-29T09:54:26Z")

</div>

And if you want to generalize it even more:

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

```

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [September 29, 2022, 1:36pm UTC](https://discourse.julialang.org/t/matrix-initialization-with-lamba-expression-missing-dimensions/87952/4 "2022-09-29T13:36:53Z")

</div>

> [@bigdio](#):
>
> Why b2 has shape (3,)?

Because it is a `Vector` of `Array`s. 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 `Matrix`es.

> [@bigdio](#):
>
> What’s the right way to use lambda expression to initialize a matrix from vectors?

One way is

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

```
