3

In the following example I am getting an array of array as an output. I would like to seek suggestion on reducing it to n-element vector.

Example: I have a vector x and then I perform subtraction on first 2 elements of the array which outputs a.

x = Float64.([1,2,3,4,5])
a= x[2,:] - x[1,:]
1-element Vector{Float64}:
 1.0

Now when i collect the following range, it returns array of array, as shown below.

c = collect(range(minimum(x).*a, maximum(x).*a, length=10))
10-element Vector{Vector{Float64}}:
 [1.0]
 [1.4444444444444444]
 [1.8888888888888888]
 [2.333333333333333]
 [2.7777777777777777]
 [3.2222222222222223]
 [3.6666666666666665]
 [4.111111111111111]
 [4.555555555555555]
 [5.0]

I would like to know how may I convert this to a vector, which can output following result?

# Expected result:
10-element Vector{Float64}:
 1.0
 1.4444444444444444
 1.8888888888888888
 2.333333333333333
 2.7777777777777777
 3.2222222222222223
 3.6666666666666665
 4.111111111111111
 4.555555555555555
 5.0

Thanks!!

2 Answers 2

4

When constructing a you want a= x[2] - x[1]. Then a will be a scalar, and everything else will behave as expected.

Sign up to request clarification or add additional context in comments.

Comments

3

In general, you can use a combination of splatting and vcat:

julia> xs = [[1],[2,3],[],[4],[5,6,7]]
5-element Vector{Vector{Any}}:
 [1]
 [2, 3]
 []
 [4]
 [5, 6, 7]

julia> vcat(xs...)
7-element Vector{Any}:
 1
 2
 3
 4
 5
 6
 7

That being said, I would follow Oscar Smith's advice and fix the root problem.

Comments

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.