I’m using a while statement to carry out calculations for as long as a condition holds. At every iteration, I calculate a vector v. I want to append every new v vector as a row vector to the previous v vector.
The problem is that my condition doesn’t have a fixed number of iterations. With every new data point, the condition can change and the size of myArray will change. I have tried creating an array of type Any; one item in v has a different type String while all other items have type Float64.
I don’t know how many iterations will take for the while loop to stop, therefore I don’t know the number of rows of my array. Because of size difference, I get an error when I try to append v to myArray.
I have tried different variations of vcat and hcat, either transposing v or not — none worked. The following piece of code explains what I’m trying to do with simpler objects.
myArray = [];
myNumber = rand(1)*100;
while myNumber > 0
# Compute vector v
v = ["a", 1, 2, 3]
# ..............................................
# Assign v to myArray as a row vector
# in every iteration. How do I do this?
# ..............................................
myNumber = myNumber - 1
end
All help is appreciated.
P.S.: At one point, I created A = Array{Any}(undef, n,4), with n large enough that it wouldn’t be a problem. At every iteration, I incremented a counter i and assigned the freshly computed v to row i, A[i,:] = v. The problem with that, besides the many unused rows, is that I tried to subset from A the part that only contained data I wanted. I relied on the type in the columns as a parameter to filter for, but I couldn’t make it work either.
Thanks for your answer. I imagine I could insert different types inside of the Tuple, so that could be a way out. But from the output, it seems that in the end you don’t have an array you could subset from, so using the push! command wouldn’t work in the way your presented. I’ll look into it, though. Maybe I can adapt it to what I need.
julia> myArray = [];
julia> myNumber = trunc(Int,(rand(1)*10)[1])
3
julia> while myNumber > 0
# Compute vector v
v = ["a", 1, 2, 3]
# ..............................................
# Assign v to myArray as a row vector
# in every iteration. How do I do this?
# ..............................................
push!(myArray,v)
myNumber = myNumber - 1
end
julia> myArray
3-element Vector{Any}:
Any["a", 1, 2, 3]
Any["a", 1, 2, 3]
Any["a", 1, 2, 3]
But maybe you want to get this?
the expected result is not very clear.
julia> stack(myArray,dims=1)
5×4 Matrix{Any}:
"a" 1 2 3
"a" 1 2 3
"a" 1 2 3
"a" 1 2 3
"a" 1 2 3
or
julia> permutedims(reduce(hcat,myArray))
5×4 Matrix{Any}:
"a" 1 2 3
"a" 1 2 3
"a" 1 2 3
"a" 1 2 3
"a" 1 2 3
You may be looking for concatenation. Look into the cat functions, and its various flavors, like hcat or vcat: Arrays · The Julia Language
A few more things:
Consider appending your data as columns, not rows. This should be more efficient.
myArray = [] creates a vector, not a matrix.
Arrays with mixed types are not terribly efficient. Using a vector of tuples, as suggested above, may be better. Or perhaps store the strings and the numbers separately.