# Create A Matrix in Julia

**URL:** <https://discourse.julialang.org/t/create-a-matrix-in-julia/21538>\
**Category:** General Usage\
**Created:** [March 6, 2019, 3:01pm UTC](https://discourse.julialang.org/t/create-a-matrix-in-julia/21538 "2019-03-06T15:01:30Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [March 14, 2019, 11:38am UTC](https://discourse.julialang.org/t/create-a-matrix-in-julia/21538/21 "2019-03-14T11:38:43Z")

</div>

> [@Elrod](#):
>
> Matrix{Float64}

to continue this work,

Qw= Matrix{Float64}  
push!(Qw[1,:,:] = CCP[1,:,:])

bugs: ERROR: syntax: invalid keyword argument name “Qw[1, :, :]”😅 @DNF

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 14, 2019, 11:55am UTC](https://discourse.julialang.org/t/create-a-matrix-in-julia/21538/22 "2019-03-14T11:55:52Z")

</div>

```julia
julia> Qw= Matrix{Float64}[]
0-element Array{Array{Float64,2},1}

julia> CPP = rand(10,11,12);

julia> push!(Qw, CPP[1,:,:]);

julia> push!(Qw, CPP[2,:,:]);

julia> Qw
2-element Array{Array{Float64,2},1}:
 [0.8935129125586814 0.3470142023127345 … 0.6738768695996915 0.6978520270709654; 0.7923192642936225 0.5547389053048148 … 0.5429744453836987 0.18337287076378694; … ; 0.4865432758891508 0.6267145315230598 … 0.532536699235963 0.7892874407659456; 0.9178282233333892 0.4930127685342016 … 0.6960617819744821 0.046685022605916116]  
 [0.4223869684524322 0.0010646995727199293 … 0.786847325799438 0.21655039764113293; 0.9000478496995337 0.8557975654465664 … 0.7634421366413904 0.7131702618107101; … ; 0.1365000421261684 0.7040795888558846 … 0.805473769012456 0.7592440782847643; 0.6405347764075793 0.11758801287203435 … 0.9800419560473239 0.43556322515432666]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 14, 2019, 2:03pm UTC](https://discourse.julialang.org/t/create-a-matrix-in-julia/21538/23 "2019-03-14T14:03:23Z")

</div>

> [@Aquaman](#):
>
> ```julia
> Qw= Matrix{Float64}[]
> push!(Qw[1,:,:] = CCP[1,:,:])
> 
> ```

`Qw` is a vector (of matrices), so you cannot index it like this `Qw[1,:,:]`, it only takes a single index. Even if it _did_ work, it would create a _copy_ which you would then `push!` to, and which then would disappear. Finally, the assignment operator `=` has no place here.

You can read about how to use `push!` by typing `?push!` in the REPL. @Elrod’s example shows correct usage.

[Previous page](https://discourse.julialang.org/t/create-a-matrix-in-julia/21538.md?page=1)
