# How to set the elements of an array of defined type in one line?

**URL:** <https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171>\
**Category:** General Usage\
**Created:** [August 11, 2021, 4:52am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171 "2021-08-11T04:52:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 4:52am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/1 "2021-08-11T04:52:01Z")

</div>

I have a simple code and a simple question.  
it is with type My\_w, and a 2-element array www whose type is My\_w,

```
mutable struct My_w
    w::Float64
end
www = Array{My_w,1}(undef,2)

```

Now, I want to set www[1] and www[2] both as My\_w(1.0). Can I do it in one line like Fortran did, such as,

```
www[:] = My_w(1.0)

```

But obviously this give me an error.

Or, do I have to do a loop for such a simple initialization like below?

```
for i in 1:2
    www[i] = My_w(1.0)
end

```

Is there a one-line ‘vectorization’ flavor solution instead of writing a loop?

* * *

Furthermore, if I have define another 2 elements array A,

```
A=zeros(2)

```

Now I want to set the elements of array A as the values of www[:].w, in fortran I can do things like,

```
A[:] = www[:].w

```

But in Julia obviously this gives me an error.

Again, in Julia do I have to do

```
for i in 1:2
   A[i] = www[i].w
end

```

Is there more concise way like Fortran did?

Dear all, as you can see, the : symbol in Fortran seems very powerful actually for array operations.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 11, 2021, 4:57am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/2 "2021-08-11T04:57:39Z")

</div>

For separate elements,

```julia
julia> www .= [My_w(1.), My_w(1.)]
2-element Vector{My_w}:
 My_w(1.0)
 My_w(1.0)

```

To reuse the same element twice,

```julia
julia> www .= Ref(My_w(1.))

```

or

```julia
julia> www .= [My_w(1.)]
2-element Vector{My_w}:
 My_w(1.0)
 My_w(1.0)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:03am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/3 "2021-08-11T05:03:03Z")

</div>

Does the Fortran code make each element reference to the same object or no? if the answer is no, then

```julia
@. ary = My_w(1.0)

```

if the answer is yes:

```julia
fill!(ary, My_w(1.0))

```

btw, it’s probably not worth to have this being mutable to begin with

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 11, 2021, 5:05am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/4 "2021-08-11T05:05:42Z")

</div>

> [@jling](#):
>
> `ary .= My_w(1.0)`

won’t work because that object has no `length` so it has to be wrapped with `Ref` or similar.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:06am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/5 "2021-08-11T05:06:33Z")

</div>

right, need to make broadcast style Ref

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 5:20am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/6 "2021-08-11T05:20:52Z")

</div>

> [@CRquantum](#):
>
> `A[:] = www[:].w`

Thank you very much, your solution 3 looks great!  
I was trying to do =. instead of .= 😅

I have added a furthermore paragraph, would you mind have a look? it is just that, is there a way to do things like

```
A[:] = www[:].w

```

Thanks!

PS as @jling pointed out, it seems the solution 3 makes www[1] and www[2] identical. I need to set them independently.

julia\> www .= [My\_w(1.)] # \<---- this is assigning the same My\_w to each element of www  
2-element Vector{My\_w}:  
My\_w(1.0)  
My\_w(1.0)

julia\> www[1].w = 3  
3

julia\> www  
2-element Vector{My\_w}:  
My\_w(3.0)  
My\_w(3.0)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:23am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/7 "2021-08-11T05:23:54Z")

</div>

```julia
A .= getproperty.(www, :w)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:26am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/8 "2021-08-11T05:26:41Z")

</div>

would you explain why do you want an array of the same My\_w object? what’s the point of mutable now that when you mutate one element you mutate all of them?

Also in this case because all of the My\_w are the same, you can technically do

```julia
A .= first(www).w

```

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 5:34am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/9 "2021-08-11T05:34:00Z")

</div>

Well, simply because I need to.  
This is just a highly simplified code and so it is easy to ask a question and find a solution.

In a Gaussian mixture model for example, you have many mixtures, you can define a type, then the whole mixtures is just an array of that type, each element has its own mean value, covariance matrix, weight, etc.

Fortran module is below,

```
module mixture
    use constants
    implicit none  
    type :: mean_covar
	    real(kind=r8), allocatable :: mu(:,:)
	    real(kind=r8), allocatable :: sigma(:,:)
	    real(kind=r8) :: w
    end type	
    type (mean_covar), public, allocatable, save :: musigma(:)
    interface assignment (=)
	    module procedure copy_mean_covar
    end interface
! +,-,*,/ are not defined, so do not use them for now.  		
contains	
    subroutine copy_mean_covar(mcl,mcr)
    type (mean_covar), intent(inout) :: mcl !left side of assignment. here need to use intent(inout)
    type (mean_covar), intent(in) :: mcr !right side of assignment
    mcl%mu(:,:)=mcr%mu(:,:)
    mcl%sigma(:,:)=mcr%sigma(:,:)
    mcl%w=mcr%w
    return
    end subroutine copy_mean_covar

    subroutine mean_covar_init(kmix,dim_p,weight,sigma,mu)
    integer(kind=i8) :: i,k,kmix,dim_p
    real(kind=r8) :: weight(kmix),sigma(kmix,dim_p,dim_p),mu(kmix,dim_p)
    allocate(musigma(kmix))
    do i = 1, kmix
	    allocate(musigma(i)%mu(dim_p,1),musigma(i)%sigma(dim_p,dim_p))  
	    musigma(i)%mu = zero
	    musigma(i)%sigma = zero
    enddo       
    do k=1,kmix
        musigma(k)%mu(1,1) = mu(k,1)
        musigma(k)%mu(2,1) = mu(k,2)
        musigma(k)%sigma(1,1) = sigma(k,1,1) 
        musigma(k)%sigma(2,2) = sigma(k,2,2) 
        musigma(k)%w = weight(k)            
    enddo
    return
    end subroutine mean_covar_init 

end module mixture

```

Then in the initialization I need to do things like,

```
call mean_covar_init(kmix,dim_p,weight,sigma,mu)

... 

wnow(:) = musigma(:)%w

```

In Fortran it is one line like above.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:36am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/10 "2021-08-11T05:36:16Z")

</div>

but right now if you change stuff in one mixture, everything in the array changes because you just have the same mixture object in that array

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 5:42am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/11 "2021-08-11T05:42:26Z")

</div>

Uhm, not sure what you mean.  
But the answer is no, each element in the array is independent, I can set the values of the fields in each element independently.  
I have an array called musigma, each element in it has a type called mean\_covar which is defined in this module.  
But like I said again, I just simplify things so that I can ask a relevant question.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:43am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/12 "2021-08-11T05:43:38Z")

</div>

```julia
julia> www .= [My_w(1.)] # <---- this is assigning the same My_w to each element of www
2-element Vector{My_w}:
 My_w(1.0)
 My_w(1.0)

julia> www[1].w = 3
3

julia> www
2-element Vector{My_w}:
 My_w(3.0)
 My_w(3.0)

```

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 5:46am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/13 "2021-08-11T05:46:10Z")

</div>

Oh, I got what you mean!  
Thanks man!

So this is not what I want.

I want www[1], www[2] to be independent.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:48am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/14 "2021-08-11T05:48:44Z")

</div>

that’s why I asked:

> [@jling](#):
>
> Does the Fortran code make each element reference to the same object or no? if the answer is no, then

and the answer I also had up there:

```julia
julia> @. www = My_w(1.) #same as www .= My_w.(1.)
2-element Vector{My_w}:
 My_w(1.0)
 My_w(1.0)

julia> www[1].w = 3
3

julia> www
2-element Vector{My_w}:
 My_w(3.0)
 My_w(1.0)

```

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 5:56am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/15 "2021-08-11T05:56:33Z")

</div>

Right right right, my bad, it’s on me 😅

The answer is of course no. There is no point to make all the elements reference to the same value all the time.

Could you please explain the @. a little bit? Sorry if it is stupid.

Also would you mind looking the my edited '‘Furthermore’ part, as below,

* * *

Furthermore, if I have define another 2 elements array A,

```
A=zeros(2)

```

Now I want to set the elements of array A as the values of www[:].w, in fortran I can do things like,

```
A[:] = www[:].w

```

But in Julia obviously this gives me an error.

Again, in Julia do I have to do

```
for i in 1:2
   A[i] = www[i].w
end

```

Is there more concise way like Fortran did?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 6:08am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/16 "2021-08-11T06:08:10Z")

</div>

did you miss the getproperty reply I had above?

`@.` makes everything in the line broadcast, in this case you can also put in the two dots individually if you want

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 11, 2021, 6:24am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/17 "2021-08-11T06:24:02Z")

</div>

Got it, thanks!  
Oh, by the way, I saw you have a ‘regular’ title, not sure what does that mean, but I guess it is like underboss or captain in Julia.  
If so, I just have a suggestion, could you please tell the boss, is it possible in the future Julia documentation pdf [https://raw.githubusercontent.com/JuliaLang/docs.julialang.org/assets/julia-1.6.2.pdf](https://raw.githubusercontent.com/JuliaLang/docs.julialang.org/assets/julia-1.6.2.pdf), at the end, add an index or something like many books did, so that it could be more convenient to locate to some Julia functions, commands, symbols stuff.  
Currently, I am kind of lost in ctrl+F when browsing the PDF

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 6:27am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/18 "2021-08-11T06:27:25Z")

</div>

haha no, regular just means I don’t have a life /s

That’s a good suggestion, I would suggest you to use the web interface which has search, or use the REPL `?` help mode.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 11, 2021, 6:59am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/19 "2021-08-11T06:59:56Z")

</div>

You could open an issue for that on JuliaLang/Julia though they might redirect you to Documenter.jl

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 11, 2021, 7:18am UTC](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171/20 "2021-08-11T07:18:20Z")

</div>

I’m surprised the comprehension constructor

```julia
www = [My_w(1) for _ in 1:2]

```

has not been mentioned.  
For `A`, the same:

```julia
A = [x.w for x in www]

```

If this absolutely has to work with preallocated arrays, the following must work:

```julia
www .= (My_w(1) for _ in eachindex(www))
A .= (x.w for x in www)

```

`map!` may be a bit of overkill for this specific task but is a very useful and powerful concept overall:

```julia
map!(x -> My_w(1), www, eachindex(www))
map!(x -> x.w, A, www)

```

[Next page](https://discourse.julialang.org/t/how-to-set-the-elements-of-an-array-of-defined-type-in-one-line/66171.md?page=2)
