Julia - How to properly concatenate with non-defined variable

Viewed 28

I want to write a loop that creates a plot and concatenates each of them. But the problem is, the variable is initially non-existent, so the concatenation doesn't work. Here is an example:

for i = 1:10
 myplot = hcat(myplot,plot(x[i],y[i]))
end

Now, I can solve this problem by doing the first iteration separately. But I was wondering if there is a more "elegant" way.

1 Answers

Use comprehension:

myplots = [plot(x[i],y[i]) for i = 1:10]

Note that if you want to rather overlay plots try the plot! function. If you want just to plot points there is a scatter function.

Related