Skip to content

Commit efd10b8

Browse files
author
Michael Abbott
committed
add rstack to readme
1 parent 6202bd3 commit efd10b8

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ stack(*, eachcol(ones(2,4)), 1:4) # == Matrix(stack(map(*, eachcol(...), 1:4
2222

2323
The same `stack_iter` method is also used for any list of arrays of heterogeneous element type,
2424
and for arrays of tuples. Notice that like `map(identity, Any[1, 1.0, 5im])`, this promotes using
25-
`promote_typejoin`, to `Number` here, rather than to `Complex{Float64}`.
25+
`promote_typejoin`, to `Number` here, rather than to `Complex{Float64}`:
2626

2727
```julia
2828
stack([1,2], [3.0, 4.0], [5im, 6im]) # isa Matrix{Number} # size(ans) == (2, 3)
@@ -33,6 +33,16 @@ The slices must all have the same `size`, but they (and the container)
3333
can have any number of dimensions. `stack` always places the slice dimensions first.
3434
There are no options.
3535

36+
### Ragged stack
37+
38+
There is also a version which does not demand that slices have equal `size` (or equal `ndims`),
39+
which always returns a new `Array`. You can control the position of slices `using OffsetArrays`:
40+
41+
```julia
42+
rstack([1:n for n in 1:10]) # upper triangular Matrix{Int}
43+
rstack(OffsetArray(fill(n,4), rand(-2:2)) for n in 1:10; fill=NaN)
44+
```
45+
3646
### Other packages
3747

3848
This one plays well with [OffsetArrays.jl](https://github.com/JuliaArrays/OffsetArrays.jl),
@@ -47,3 +57,5 @@ Besides which, there are several other ways to achieve similar things:
4757
* For a generator of arrays, the built-in `reduce(hcat,...)` may work, but it slow compared to `stack`: see [test/speed.jl](test/speed.jl) for some examples.
4858

4959
The package [ArraysOfArrays.jl](https://oschulz.github.io/ArraysOfArrays.jl/stable/#section_ArrayOfSimilarArrays-1) solves the opposite problem, of accessing one large array as if it were many slices. As does [`JuliennedArrays.Slices`](https://bramtayl.github.io/JuliennedArrays.jl/latest/#JuliennedArrays.Slices-Union{Tuple{NumberOfDimensions},%20Tuple{Item},%20Tuple{AbstractArray{Item,NumberOfDimensions},Vararg{Int64,N}%20where%20N}}%20where%20NumberOfDimensions%20where%20Item), and of course [`Base.eachslice`](https://docs.julialang.org/en/v1/base/arrays/#Base.eachslice).
60+
61+
Finally, after writing this I learned of [julia/31644](https://github.com/JuliaLang/julia/pull/31644) which extends `reduce(hcat,...)` to work on generators.

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ end
196196
@test stack(*, _eachcol(ones(2,4)), 1:4) isa Matrix{Float64}
197197
@test stack([1,2], [3.0, 4.0], [5im, 6im]) isa Matrix{Number}
198198

199+
@test rstack([1:n for n in 1:10]) isa Matrix{Int}
200+
@test rstack(OffsetArray(fill(n,4), rand(-2:2)) for n in 1:10; fill=NaN) isa OffsetArray{Real,2}
201+
199202
end
200203
@testset "vstack" begin
201204

0 commit comments

Comments
 (0)