I’m struggling with what seems like a simple coding pattern, but I seem unable to come up with a clean solution to it. Specifically, I often find myself needing to a) create a new object (such as a matrix) and then add to it inside an iteration.
What I’d like to do is something like the following:
a = Array{Float64}
while (don't exit)
append!(a, <stuff>)
end
where the first line would create an empty object and append() would add values to it, even if it’s initially empty.
But of course that doesn’t work – the call to append!() barfs. What I have to do instead is something like
a = nothing
while (don't exit)
if a == nothing
a = Array{Float64}(<initial stuff>)
else
append!(a, <new stuff>)
end
end
…which, obviously, feels really clunky.
Yes, I could - in some cases - pre-allocate an uninitialized array of the right size, but that isn’t always possible or desirable. For instance, Flux/Zygote doesn’t let you modify an array in place, because it screws with automatic differentiation. In some cases you don’t know how big the object is going to be before you start the iteration.
I’m currently having this same issue with generating a plot. I want to just create an empty plot and then add different data lines to it. But I can’t figure out how to generate a blank plot.
a = Array{Float64}[]
append!(a, ([1, 2],)) # 1-element Vector{Array{Float64}}: [1.0, 2.0]
append!(a, ([4, 5],[6, 7]))
# can transform into other shapes needed, for example:
m = reduce(hcat, a)'
Without knowing what you are actually trying to accomplish, it’s
The basic question here is why are you dynamically appending rows to a multidimensional array (if that is indeed what you want)? What are you actually trying to accomplish? Without knowing that, it’s hard to say whether you should be using an array-of-arrays approach, ElasticArrays or similar, an array of SVector, or …
In this particular case reading data from a file. The data consists of entries, each of which is a matrix. I’d like to accumulate all of it into a three-dimensional array.
In principle, I don’t really know how big each entry is; all I know is that all the entries are of the same size. I also don’t know how many of them there are. Figuring out the size of an entry is as simple as reading the first one. Figuring out how many of them there are would require traversing the entire file.
But this is a pain point that I seem to encounter often. In another part of this codebase I’m assembling a grid of plots which will show the data. I don’t know how many plots before runtime. I’d like to create an empty plot grid, probably with a known number of columns but an unknown number of rows, and keep appending plots to the end until I’m done.
Why should it work when calling lineplot()? It seems that there are no examples in the manual for subplotting using the results from this command.
All examples use plot() or scatter() for that purpose.
julia> using Plots
help?> lineplot
search:
Couldn't find lineplot
Perhaps you meant plot
No documentation found.
Binding lineplot does not exist.
It appears this is a function from UnicodePlots, not from Plots. You seem to misunderstand how Plots and its backends work - you just do using Plots and then choose your backend by calling the respective function as you have done above with unicodeplots(). You do not then do using UnicodePlots and mix functions defined in the backend plotting package with those defined in Plots.
Plotting multiple plots onto one figure is documented here (granted it does not include an example of using the splatting operator but then one could argue that’s just a basic language feature that Plots.jl doesn’t have to document separately).