# How to work on a specific field of an vector of structure?

**URL:** <https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 18, 2022, 7:05pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720 "2022-02-18T19:05:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 7:05pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/1 "2022-02-18T19:05:15Z")

</div>

I have the below `R` instance of `Rs` structure. I want to work on a specific field of all rows (as dealing with Matrix). So, I am using `R[:].I[:]` but it gives an error. Any way to do that?

```julia
Base.@kwdef mutable struct Rs
    I::Vector{Float64} = [2,2,2]
    Dot::Array{Float64, 2} = [0 0 0; 0 0 0; 0 0 0]
end
R = Rs[];
push!(R, Rs());
push!(R, Rs());

julia> R[:].I[:]
ERROR: type Array has no field I
Stacktrace:
 [1] getproperty(x::Vector{Rs}, f::Symbol)
   @ Base .\Base.jl:33
 [2] top-level scope
   @ REPL[1]:1

```

---

<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:** [February 18, 2022, 7:22pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/2 "2022-02-18T19:22:05Z")

</div>

`R` is a vector, and doesn’t have a field, `I`. You can do `R[i].I` to access the field of the ith element of `R`. To get a vector of all the `I`s, you can try

```julia
getproperty.(R, :I) 

```

`R[:]` doesn’t do anything useful, it just returns a vector of all the elements of `R`, so it’s equal to `R` itself.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 7:31pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/3 "2022-02-18T19:31:41Z")

</div>

@DNF thank you very much. Using `getproperty.(R, :I)`, return 2-elemnt Vector of vector, as below:

```julia
julia> getproperty.(R, :I)
2-element Vector{Vector{Float64}}:
 [2.0, 2.0, 2.0]
 [2.0, 2.0, 2.0]

```

Can I do something to return this results as Array{2, Float64}, i.e.

```julia
2×3 Matrix{Float64}:
 2.0 2.0 2.0 # as R[1].I
 2.0 2.0 2.0 # as R[2].I

```

---

<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:** [February 18, 2022, 8:26pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/4 "2022-02-18T20:26:08Z")

</div>

You’ll have to explicitly combine them. You should not put them along rows, though, but columns.

Maybe

```julia
reduce(hcat, getproperty(r, :I) for r in R) 

```

Untested.

---

<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:** [February 18, 2022, 8:29pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/6 "2022-02-18T20:29:15Z")

</div>

Typo

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 8:31pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/7 "2022-02-18T20:31:53Z")

</div>

Thank you very much, but it gives the results transposed

```julia
julia> reduce(hcat, getproperty(r, :I) for r in R)
3×2 Matrix{Float64}:
 2.0 2.0
 2.0 2.0
 2.0 2.0

```

---

<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:** [February 18, 2022, 10:19pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/8 "2022-02-18T22:19:53Z")

</div>

Yes, as I said, this is the correct orientation. Julia Arrays are column major, so things should be laid out in column order.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 11:04pm UTC](https://discourse.julialang.org/t/how-to-work-on-a-specific-field-of-an-vector-of-structure/76720/9 "2022-02-18T23:04:18Z")

</div>

Ok, thank you very much!
