# Easier way to assign a variable to a row of an array?

**URL:** <https://discourse.julialang.org/t/easier-way-to-assign-a-variable-to-a-row-of-an-array/45545>\
**Category:** General Usage\
**Tags:** arrays, distributions\
**Created:** [August 26, 2020, 2:29am UTC](https://discourse.julialang.org/t/easier-way-to-assign-a-variable-to-a-row-of-an-array/45545 "2020-08-26T02:29:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaepil\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaepil_lee/32/9146_2.png) [@Jaepil\_Lee](https://discourse.julialang.org/u/Jaepil_Lee)\
**Post date:** [August 26, 2020, 2:29am UTC](https://discourse.julialang.org/t/easier-way-to-assign-a-variable-to-a-row-of-an-array/45545/1 "2020-08-26T02:29:17Z")

</div>

Hello Julia users!

I am trying to make a hypothetical dataset of variables X2, Z1, Z2, and ε with some mean and covariance. Using `Distributions` and `Random` packages works just fine, but I was wondering if I can assign the variable names and generating an array at the same time.

```julia
using LinearAlgebra
using Statistics, Distributions, Random
Random.seed!(123)
μ = [0.0; 0.0; 0.0; 0.0]; # order: X2, Z1, Z2, ε
Σ = [1.0 0.3 -.4 0.5; 
     0.3 1.0 0.4 0.0;
     -.4 0.4 1.0 0.0;
     0.5 0.0 0.0 3.0];
N = 2000;
XZε = rand(MvNormal(μ,Σ),N)
X2 = XZε[1,:]
Z1 = XZε[2,:]
Z2 = XZε[3,:]
ε = XZε[4,:]

```

As you can see, variable names X2, Z1, Z2, and ε are assigned after XZε is created. Is there any way I can do that in fewer codes?

Thank you for your input!

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [August 26, 2020, 3:42am UTC](https://discourse.julialang.org/t/easier-way-to-assign-a-variable-to-a-row-of-an-array/45545/2 "2020-08-26T03:42:22Z")

</div>

If you’re OK with them being views (which might yield better performance actually, depending what you do next),

```julia
X2,Z1,Z2,ε = eachrow(XZε)

```

or if you want bona-fide arrays:

```julia
X2,Z1,Z2,ε = collect.(eachrow(XZε))

```

---

<div class="post-metadata">

**Author:** ![Jaepil\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaepil_lee/32/9146_2.png) [@Jaepil\_Lee](https://discourse.julialang.org/u/Jaepil_Lee)\
**Post date:** [August 26, 2020, 4:15pm UTC](https://discourse.julialang.org/t/easier-way-to-assign-a-variable-to-a-row-of-an-array/45545/3 "2020-08-26T16:15:28Z")

</div>

Thank you for your suggestion! I wanted to use the generated rows to show students different linear instrumental variables estimation. When would `view` provide better performance, and would it be the case for me too (as in, I am just gonna `hcat()` some of the rows and do basic linear algebra)?
