3

I don't know how to make an array of vectors and matrices in Julia. For example, how I make a list p such that

p[1]=[1;2]
p[2]=[2 3; 4 5]

?

1 Answer 1

3

You can use an array of "Any"

p=Any[[1;2],[2 3; 4 5]]

which returns

2-element Array{Any,1}:
 [1, 2]
 [2 3; 4 5]
Sign up to request clarification or add additional context in comments.

3 Comments

As a small comment - adding Any is crucial here, as if you omit it Julia in some cases might perform type conversion when constructing a wrapper vector. Here is an example when this happens (with omitted Any): [[1.0], [1]].
Well, it's not crucial here, since type conversion won't happen. Also, if you just write [[1,2], [2 3; 4 5]] you get a tighter type bound, Any might be undesirable. Perhaps Array[[1,2], [2 3; 4 5]] or Array{Int}[[1,2], [2 3; 4 5]] are better options.
In this particular example it will not happen indeed, but it would in general. As @sotowa wants to mix vectors and matrices the most efficient in this case would be Union{Vector{Int}, Matrix{Int}}[[1;2],[2 3; 4 5]] assuming only Vector{Int} and Matrix{Int} elements would be passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.