# Converting array to a vector

**URL:** <https://discourse.julialang.org/t/converting-array-to-a-vector/34881>\
**Category:** General Usage\
**Tags:** arrays, vector, convert\
**Created:** [February 19, 2020, 11:36pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881 "2020-02-19T23:36:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [February 19, 2020, 11:36pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/1 "2020-02-19T23:36:22Z")

</div>

I have an array T which is 76×1 Array{Any,2}. How can I convert it into 76-element Array{Float64,1} or a 76 element row vector? This array contains data only in rows.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 19, 2020, 11:43pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/2 "2020-02-19T23:43:50Z")

</div>

To convert it to 1d, use `vec(T)` (1d array = “column” vector) or `vec(T)'` (row vector). If you need to convert the element type as well, you can use e.g. `Vector{Float64}(vec(T))`.

---

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [February 20, 2020, 12:23am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/3 "2020-02-20T00:23:20Z")

</div>

Thank you. I am trying to calculate the internal rate of return using [NPFinancial](https://github.com/tk3369/NPFinancial.jl) package.

I have tried your suggestion, but for some reason I am getting errors. For example, if I use the code below:

> using NPFinancial  
> T1=vec(T)  
> Return=irr(T1)

This gives the following error:  
MethodError: no method matching Polynomials.Poly(::Array{Any,1})  
Closest candidates are:  
Polynomials.Poly(!Matched::AbstractArray{T,1}) where T\<:Number at C:\Users\a.julia\packages\Polynomials\fjDRE\src\Polynomials.jl:75  
Polynomials.Poly(!Matched::AbstractArray{T,1}, !Matched::Union{Char, AbstractString, Symbol}) where T\<:Number at C:\Users\a.julia\packages\Polynomials\fjDRE\src\Polynomials.jl:75  
Polynomials.Poly(!Matched::Number) at C:\Users\a.julia\packages\Polynomials\fjDRE\src\Polynomials.jl:86  
…

Stacktrace:  
[1] irr(::Array{Any,1}) at C:\Users\a.julia\packages\NPFinancial\xeikO\src\NPFinancial.jl:233  
[2] top-level scope at In[20]:6

Also, using the code below to convert element type as well is giving errors messages:

> Vector{Float64}(vec(T))

MethodError: Cannot `convert` an object of type Adjoint{Any,Array{Any,2}} to an object of type Float64  
Closest candidates are:  
convert(::Type{T}, !Matched::T) where T\<:Number at number.jl:6  
convert(::Type{T}, !Matched::Number) where T\<:Number at number.jl:7  
convert(::Type{T}, !Matched::Base.TwicePrecision) where T\<:Number at twiceprecision.jl:250  
…

Stacktrace:  
[1] setindex!(::Array{Float64,1}, ::Adjoint{Any,Array{Any,2}}, ::Int64) at .\array.jl:782  
[2] copyto!(::IndexLinear, ::Array{Float64,1}, ::IndexLinear, ::Array{Any,1}) at .\abstractarray.jl:807  
[3] copyto! at .\abstractarray.jl:799 [inlined]  
[4] Array{Float64,1}(::Array{Any,1}) at .\array.jl:498  
[5] top-level scope at In[19]:3

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 20, 2020, 12:25am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/4 "2020-02-20T00:25:12Z")

</div>

What’s in your array? How do you make it?

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [February 20, 2020, 1:48am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/5 "2020-02-20T01:48:33Z")

</div>

You can also use

```julia
 A=dropdims(A, dims = 2)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 20, 2020, 6:19am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/6 "2020-02-20T06:19:26Z")

</div>

> [@bandaya](#):
>
> For example, if I use the code below

Please provide an MWE: we don’t know what `T` is.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [February 20, 2020, 10:15pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/7 "2020-02-20T22:15:56Z")

</div>

Here is a simplified example. This reads a spreadsheet containing 10,10,110 in cells A1:C1 and stores it an array P which is a 3×1 Adjoint{Any,Array{Any,2}}

Array T is created by concatenating array P with -100

```julia
using LinearAlgebra
using XLSX
using NPFinancial

```

```julia
julia> xf = XLSX.readxlsx("C:/Users/a/Documents/Julia/Data2.xlsx")
XLSXFile("Data2.xlsx") containing 1 Worksheets
             sheetname size range
-------------------------------------------------
               Sheet1 1x3 A1:C1

```

```julia
julia> S=xf["Sheet1"]
1×3 XLSX.Worksheet: ["Sheet1"](A1:C1)

```

```julia
julia> P=S["A1:C1"]'
3×1 Adjoint{Any,Array{Any,2}}:
  10
  10
 110

```

```julia
> julia> T=[-100;P]
4×1 Array{Any,2}:
 -100
   10
   10
  110

```

```julia
julia> Return=irr(T)
ERROR: MethodError: no method matching Polynomials.Poly(::Array{Any,2})

```

I am not sure why reading from a spreadsheet creates this 3×1 Adjoint{Any,Array{Any,2}} rather than a 3-element Array{Int64,1} or 3-element Array{Float64,1}.

The source of error seems to be that the array is Any and not Float64 or Int, and its dimension. As a workaround, the following code seems to make it work but it probably not an efficient way.

```julia
T=dropdims(T,dims=2)
T=Vector{Float64}(vec(T))
Return = irr(T)

```

Is there a better way of of coding this so that the issue with the type and dimension of the array doesn’t need to be fixed manually?

Also, this workaround is not working on the full data set that I am using. dropdims gives the following error  
ArgumentError: dropped dims must be in range 1:ndims(A)

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [February 21, 2020, 12:29am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/8 "2020-02-21T00:29:41Z")

</div>

Maybe worth knowing that `map` and comprehensions try to work out the right type to return, and so will remove `Any`:

```julia
julia> A = Array{Any}(undef, 1,3)'; A .= rand(1:99, 3); A
3×1 LinearAlgebra.Adjoint{Any,Array{Any,2}}:
 76
 66
 75

julia> copy(A)
3×1 Array{Any,2}:
 76
 66
 75

julia> map(identity, A)
3×1 Array{Int64,2}:
 76
 66
 75

julia> vec(map(identity, A))
3-element Array{Int64,1}:
 76
 66
 75

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 21, 2020, 6:11am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/9 "2020-02-21T06:11:08Z")

</div>

I would just convert explicitly, eg something like

```julia
irr(vec(Float64.(T)))

```

---

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [February 21, 2020, 12:40pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/10 "2020-02-21T12:40:49Z")

</div>

If I use, `irr(vec(Float64.(T)))` it works fine for the simple example that I provided, but when used on the actual data it gives me the following error:

```julia
MethodError: no method matching Float64(::Adjoint{Any,Array{Any,2}})
Closest candidates are:
  Float64(!Matched::Int8) at float.jl:60
  Float64(!Matched::Int16) at float.jl:60
  Float64(!Matched::Int32) at float.jl:60
  ...

Stacktrace:
 [1] _broadcast_getindex_evalf at .\broadcast.jl:630 [inlined]
 [2] _broadcast_getindex at .\broadcast.jl:603 [inlined]
 [3] getindex at .\broadcast.jl:563 [inlined]
 [4] copyto_nonleaf!(::Array{Float64,1}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},Type{Float64},Tuple{Base.Broadcast.Extruded{Array{Any,1},Tuple{Bool},Tuple{Int64}}}}, ::Base.OneTo{Int64}, ::Int64, ::Int64) at .\broadcast.jl:1003
 [5] copy at .\broadcast.jl:857 [inlined]
 [6] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,Type{Float64},Tuple{Array{Any,1}}}) at .\broadcast.jl:819
 [7] top-level scope at In[10]:3

```

I have shown contents of array T based on the actual data below:

````julia
Any[-1.6043292547551804e9, Any[7.993926967188627e7; 7.577525249380158e7; 6.583937323458159e7; 8.608217751388194e7; 9.69128127886691e7; 6.437005981273358e7; 7.289126611075686e7; 7.492735287822661e7; 8.758292626045902e7; 8.811690625234096e7; 9.595982933138174e7; 8.397463619409351e7; 7.665558393297486e7; 7.55521260278017e7; 5.943558255788591e7; 6.487489846620125e7; 5.258234111810494e7; 5.798967421622807e7; 4.930154414296609e7; 4.333388571692812e7; 4.786619300874222e7; 4.326364481328481e7; 4.936017238855486e7; 4.198739279252242e7; 7.685356416590588e7; 4.462483732473162e7; 3.337725602834007e7; 2.1331982993840132e7; 3.208723678076784e7; 1.4126897095534535e7; 1.1356006387483776e7; 8.572517645603871e6; 8.349558443671445e6; 6.213681616639372e6; 5.7977062870941e6; 5.801479131366041e6; 8.62706228970659e6; 8.151591197165552e6; 4.265853176992651e6; 2.936500324111242e6; 2.897981340424616e6; 5.062061253386277e6; 2.660583729745694e6; 2.3283476823908756e6; 1.65658209109371e6; 4.4514590641519325e6; 624845.5938319229; 756596.3688816014; 1.7411659890081713e6; 716398.6933130373; 454796.50186683424; 456408.35177196824; 459653.74825904454; 1.926965253458244e6; 381823.98343152396; 320594.90363532107; 280336.13621770905; 283512.10068234045; 283980.999272435; 284483.7995077695; 285244.0757536114; 268087.47167585994; 571485.6298871347; 594296.0978518437; 114278.06519121297; 77662.01438359109; 77998.80239296904; 78352.50892616561; 78723.48795868176; 79112.09879134771; 0.0; 0.0; 0.0; 0.0; 0.0]] ```
````

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 21, 2020, 12:59pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/11 "2020-02-21T12:59:03Z")

</div>

That’s not an array, that’s an array of a scalar and a matrix. Something like

```julia
Float64.(vcat(T[1], vec(T[2])))

```

could work.

You may want to read the manual, these kind of data manipulations are very basic and it will be frustrating to use Julia without being aware of them.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 21, 2020, 1:09pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/12 "2020-02-21T13:09:11Z")

</div>

For that sort of data you can use `Iterators.flatten`:

```julia
julia> Float64.(Iterators.flatten(T))
76-element Array{Float64,1}:
    -1.6043292547551804e9
     7.993926967188627e7 
     7.577525249380158e7 
     6.583937323458159e7 
     8.608217751388194e7 
     9.69128127886691e7  
     6.437005981273358e7 
     7.289126611075686e7 
     7.492735287822661e7 
     8.758292626045902e7 
     ⋮                   
 77998.80239296904       
 78352.50892616561       
 78723.48795868176       
 79112.09879134771       
     0.0                 
     0.0                 
     0.0                 
     0.0                 
     0.0 

```

---

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [February 21, 2020, 11:04pm UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/13 "2020-02-21T23:04:35Z")

</div>

Thanks. I will review the online documentation, but some topics are not easy for a novice to grasp with. For example, reading the documentation it is not clear me to what is meant by “Iterators” in “Iterators.flatten“ as mentioned in one of the responses

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 22, 2020, 6:18am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/14 "2020-02-22T06:18:37Z")

</div>

It is a module name, see [Modules · The Julia Language](https://docs.julialang.org/en/v1/manual/modules/)

---

<div class="post-metadata">

**Author:** ![tallakt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tallakt/32/46978_2.png) [@tallakt](https://discourse.julialang.org/u/tallakt)\
**Post date:** [April 14, 2023, 7:15am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/15 "2023-04-14T07:15:50Z")

</div>

```julia
list = [1, 2, 3, 4, 5]
matrix = reshape(list, (1, length(list)))

```

The list seems to already be a vector of sorts.

Another option is

```julia
collect(list')

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 14, 2023, 7:29am UTC](https://discourse.julialang.org/t/converting-array-to-a-vector/34881/16 "2023-04-14T07:29:58Z")

</div>

> [@tallakt](#):
>
> `reshape(list, (1, length(list)))`

Julia makes this easier:

```julia
reshape(list, 1, :)

```
