Let's say I have an array of vectors:
""" simple line equation """
function getline(a::Array{Float64,1},b::Array{Float64,1})
line = Vector[]
for i=0:0.1:1
vector = (1-i)a+(i*b)
push!(line, vector)
end
return line
end
This function returns an array of vectors containing x-y positions
Vector[11]
> Float64[2]
> Float64[2]
> Float64[2]
> Float64[2]
.
.
.
Now I want to seprate all x and y coordinates of these vectors to plot them with plotyjs.
I have already tested some approaches with no success! What is a correct way in Julia to achive this?
You can broadcast
getindex
:Edit 3:
Alternatively, use list comprehensions:
Edit:
For performance reasons, you should use
StaticArrays
to represent 2D points. E.g.:Broadcasting
getindex
and list comprehensions will still work, but you can alsoreinterpret
the vector as a2×11
matrix:Note that this does not copy the data. You can simply use
m
directly or define, e.g.,Edit 2:
Btw., not restricting the type of the arguments of the
getline
function has many advantages and is preferred in general. The version above will work for any type that implements multiplication with a scalar and addition, e.g., a possible implementation ofimmutable Point ... end
(making it fully generic will require a bit more work, though).