Last active
April 1, 2019 00:31
-
-
Save singularitti/262115e7f03cbe3538749a05534203a6 to your computer and use it in GitHub Desktop.
Unfold a nested Julia array #Julia #array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For a detailed explanation, please see here: | |
# https://discourse.julialang.org/t/how-do-you-unfold-a-nested-julia-array/2243/7?u=singularitti | |
function deepflatten(arr::Vector{<: Vector}) | |
dim = [1] | |
function recursiveflatten(arr, dim) | |
if arr isa Vector{<: Vector} | |
recursiveflatten(collect(Iterators.flatten(arr)), | |
pushfirst!(dim, length(arr) / prod(dim))) | |
else | |
arr, pushfirst!(dim, length(arr) / prod(dim)) | |
end | |
end | |
flattened, dim = recursiveflatten(arr, dim) | |
reshape(flattened, dim[1:end - 1]...) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment