# Assign a value for one dimension of multi-dimensional array

**URL:** <https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998>\
**Category:** New to Julia\
**Tags:** arrays\
**Created:** [June 29, 2023, 8:57pm UTC](https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998 "2023-06-29T20:57:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [June 29, 2023, 8:57pm UTC](https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998/1 "2023-06-29T20:57:27Z")

</div>

Hi, I’m a bit lost with the following simple exercise. I was wondering what would be the most efficient way to achieve this.

Suppose I have zeros(5,3,2), which returns the following.

```julia
5×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

[:, :, 2] =
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

```

I want to change the third column of each matrix by its index of the third array likewise :

```julia
A = zeros(5,3,2)

5×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 1

[:, :, 2] =
 0.0 0.0 2
 0.0 0.0 2
 0.0 0.0 2
 0.0 0.0 2
 0.0 0.0 2

```

I wondered if there is a similar syntax to `A[any, any,1] .= 1` and if `vcat(A[:,:,1],A[:,:,2])` could be a fast way to get the following final result.

```julia
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 1
 0.0 0.0 2
 0.0 0.0 2
 0.0 0.0 2
 0.0 0.0 2
 0.0 0.0 2

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [June 29, 2023, 9:46pm UTC](https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998/2 "2023-06-29T21:46:46Z")

</div>

I think a `for` loop is probably simplest for this.

```julia
julia> x = zeros(5,3,2);

julia> for k in axes(x,3)
               x[:,end,k] .= k
       end

julia> x
5×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0

[:, :, 2] =
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0

```

I don’t fully comprehend your last question, but you can achieve your last result using

```julia
julia> reduce(vcat,eachslice(x,dims=3))
10×3 Matrix{Float64}:
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0

```

It seems like it might have been simpler to have used a 2-vector of 5x3 matrices to begin with. Unlike MATLAB, NumPy, and some other platforms, it isn’t necessary to vectorize operations to get fast code when using Julia.

You also could assemble your final matrix directly like

```julia
julia> y = zeros(10,3);

julia> for i in axes(y,1)
               y[i,end] = div(i-1,5)+1
       end

julia> y
10×3 Matrix{Float64}:
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 30, 2023, 7:50am UTC](https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998/3 "2023-06-30T07:50:58Z")

</div>

You can construct the first result via the following.

```julia
julia> [1,1,1,1,1] .* [0;; 0;; 1] .* [1. ;;; 2]
5×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0
 0.0 0.0 1.0

[:, :, 2] =
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0
 0.0 0.0 2.0

```

Here is another way to construct the second result.

```julia
julia> [(I[2]==3)*(1 + (I[1] > 5)) for I in CartesianIndices((10,3))]
10×3 Matrix{Int64}:
 0 0 1
 0 0 1
 0 0 1
 0 0 1
 0 0 1
 0 0 2
 0 0 2
 0 0 2
 0 0 2
 0 0 2

```

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [June 30, 2023, 8:21am UTC](https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998/4 "2023-06-30T08:21:56Z")

</div>

Thank you very much for the suggestion. I didn’t know the usage of ;; and ;;;. It’s awesome!

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [June 30, 2023, 8:22am UTC](https://discourse.julialang.org/t/assign-a-value-for-one-dimension-of-multi-dimensional-array/100998/5 "2023-06-30T08:22:21Z")

</div>

Thank you very much. It workers slightly faster with your suggested version. I’ll use it.
