Skip to content

Instantly share code, notes, and snippets.

@singularitti
Last active April 1, 2019 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singularitti/262115e7f03cbe3538749a05534203a6 to your computer and use it in GitHub Desktop.
Save singularitti/262115e7f03cbe3538749a05534203a6 to your computer and use it in GitHub Desktop.
Unfold a nested Julia array #Julia #array
# 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