You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype Foo:
24
-
Foo{Int64}(1, 10) Foo{Int64}(2, 20)
25
-
Foo{Int64}(3, 30) Foo{Int64}(4, 40)
12
+
```@repl intro
13
+
using StructArrays
14
+
struct Foo{T}
15
+
a::T
16
+
b::T
17
+
end
18
+
adata = [1 2; 3 4]; bdata = [10 20; 30 40];
19
+
x = StructArray{Foo}((adata, bdata))
26
20
```
27
21
28
22
You can also initialze a StructArray by passing in a NamedTuple, in which case the name (rather than the order) specifies how the input arrays are assigned to fields:
29
23
30
-
```jldoctest intro
31
-
julia> x = StructArray{Foo}((b = adata, a = bdata)) # initialize a with bdata and vice versa
32
-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype Foo:
33
-
Foo{Int64}(10, 1) Foo{Int64}(20, 2)
34
-
Foo{Int64}(30, 3) Foo{Int64}(40, 4)
24
+
```@repl intro
25
+
x = StructArray{Foo}((b = adata, a = bdata)) # initialize a with bdata and vice versa
35
26
```
36
27
37
28
If a struct is not specified, a StructArray with Tuple or NamedTuple elements will be created:
38
-
```jldoctest intro
39
-
julia> x = StructArray((adata, bdata))
40
-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype Tuple{Int64, Int64}:
41
-
(1, 10) (2, 20)
42
-
(3, 30) (4, 40)
43
-
44
-
julia> x = StructArray((a = adata, b = bdata))
45
-
2×2 StructArray(::Matrix{Int64}, ::Matrix{Int64}) with eltype NamedTuple{(:a, :b), Tuple{Int64, Int64}}:
46
-
(a = 1, b = 10) (a = 2, b = 20)
47
-
(a = 3, b = 30) (a = 4, b = 40)
29
+
```@repl intro
30
+
x = StructArray((adata, bdata))
31
+
x = StructArray((a = adata, b = bdata))
48
32
```
49
33
50
34
It's also possible to create a `StructArray` by choosing a particular dimension to interpret as the components of a struct:
51
35
52
-
```jldoctest intro
53
-
julia> x = StructArray{Complex{Int}}(adata; dims=1) # along dimension 1, the first item `re` and the second is `im`
54
-
2-element StructArray(view(::Matrix{Int64}, 1, :), view(::Matrix{Int64}, 2, :)) with eltype Complex{Int64}:
55
-
1 + 3im
56
-
2 + 4im
57
-
58
-
julia> x = StructArray{Complex{Int}}(adata; dims=2) # along dimension 2, the first item `re` and the second is `im`
59
-
2-element StructArray(view(::Matrix{Int64}, :, 1), view(::Matrix{Int64}, :, 2)) with eltype Complex{Int64}:
60
-
1 + 2im
61
-
3 + 4im
36
+
```@repl intro
37
+
x = StructArray{Complex{Int}}(adata; dims=1) # along dimension 1, the first item `re` and the second is `im`
38
+
x = StructArray{Complex{Int}}(adata; dims=2) # along dimension 2, the first item `re` and the second is `im`
62
39
```
63
40
64
41
One can also create a `StructArray` from an iterable of structs without creating an intermediate `Array`:
65
42
66
-
```jldoctest intro
67
-
julia> StructArray(log(j+2.0*im) for j in 1:10)
68
-
10-element StructArray(::Vector{Float64}, ::Vector{Float64}) with eltype ComplexF64:
69
-
0.8047189562170501 + 1.1071487177940904im
70
-
1.0397207708399179 + 0.7853981633974483im
71
-
1.2824746787307684 + 0.5880026035475675im
72
-
1.4978661367769954 + 0.4636476090008061im
73
-
1.683647914993237 + 0.3805063771123649im
74
-
1.8444397270569681 + 0.3217505543966422im
75
-
1.985145956776061 + 0.27829965900511133im
76
-
2.1097538525880535 + 0.24497866312686414im
77
-
2.2213256282451583 + 0.21866894587394195im
78
-
2.3221954495706862 + 0.19739555984988078im
43
+
```@repl intro
44
+
StructArray(log(j+2.0*im) for j in 1:10)
79
45
```
80
46
81
47
Another option is to create an uninitialized `StructArray` and then fill it with data. Just like in normal arrays, this is done with the `undef` syntax:
0 commit comments