# Create view for an Array of FieldVector

**URL:** https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649
**Category:** General Usage
**Tags:** question
**Created:** [March 6, 2020, 4:32pm UTC](https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649 "2020-03-06T16:32:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)
#### Post date: [March 6, 2020, 4:32pm UTC](https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649/1 "2020-03-06T16:32:51Z")

</div>

Let’s say I have a struct that represents a point in 3D:

```julia
using StaticArrays
struct Point3D{T} <: FieldVector{3, T}
    x::T
    y::T
    z::T
end

```

Then I generate some points like:

`points = rand(Point3D{Float64}, 10)`

If I am interested in the y-components only, I could for example do this:

```julia
pointsMatrix = reshape(reinterpret(Float64, points), 3, 10)
y = view(pointsMatrix, 2, :)

```

Is there a shorter / more direct way to do this (i.e. without reinterpret and reshape)?

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 6, 2020, 4:49pm UTC](https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649/2 "2020-03-06T16:49:59Z")

</div>

I am not sure how this fits into views or not but one maybe easier way is to use `StructArrays`

```julia
using StructArrays
S = StructArray(points)
S.y

```

They are pretty performant even if it isn’t a `view`

---

<div class="post-metadata">

### Author: ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)
#### Post date: [March 6, 2020, 4:54pm UTC](https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649/3 "2020-03-06T16:54:27Z")

</div>

Interesting, I am not familiar with StructArrays. I will have a look, but I do prefer a solution which doesn’t require an additional package.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 6, 2020, 5:00pm UTC](https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649/4 "2020-03-06T17:00:08Z")

</div>

Regarding your original solution, I think you cannot get around the `reinterpret` but you probably don’t need to `reshape`.

---

<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 6, 2020, 5:14pm UTC](https://discourse.julialang.org/t/create-view-for-an-array-of-fieldvector/35649/5 "2020-03-06T17:14:21Z")

</div>

StructArrays will copy, though, it seems. In that case you might as well do

```julia
getindex.(points, 2)

```
