# How to divide vector elements in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-divide-vector-elements-in-julia/62274>\
**Category:** New to Julia\
**Created:** [June 2, 2021, 1:07pm UTC](https://discourse.julialang.org/t/how-to-divide-vector-elements-in-julia/62274 "2021-06-02T13:07:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 2, 2021, 1:07pm UTC](https://discourse.julialang.org/t/how-to-divide-vector-elements-in-julia/62274/1 "2021-06-02T13:07:02Z")

</div>

Hello,  
I have an array composed by a vector of two elements: (in the form `[1, 2], [3,4],...`). I would like to calculate the percentage of each element, so I calculated the sum of the vector (obtaining something in the form of: `[3], [7]...`). Then I tried to divide the vector of the first elements by this some (to get `[0.33], [0.42],...`) but instead I got a matrix.  
How do I properly divide vectors in Julia?  
The example is:

```julia
julia> soln.u
32-element Vector{Vector{Float64}}:
 [1000.0, 1000.0]
 [1367.6600181427432, 1279.2801193162964]
 [2470.4796891385, 2036.944188468805]
 [5114.450555250719, 3610.615999603425]
 [11601.210411658707, 6877.441429534098]
 [29202.513110268377, 14220.546829596376]
 [79936.7711904854, 31426.650975298417]
 [236625.52371589997, 74006.8620996869]
 [745384.4693280015, 184282.48911711766]
 [2.4437472687509363e6, 485312.9405044525]
 [7.922433143990216e6, 1.3788188022842316e6]
 [2.1877766766866736e7, 4.331519191847759e6]
 [4.060014627282435e7, 1.2183659848120349e7]
 [6.2777968383830175e7, 3.423140611354349e7]
 [7.916321639200333e7, 6.267655325823425e7]
 [9.989493619123092e7, 1.0705359324403352e8]
 [1.1819430058769086e8, 1.4667971299850982e8]
 [1.356524642999164e8, 1.8243163613064378e8]
 [1.5146396530996737e8, 2.1300436252651647e8]
 [1.6642929351258355e8, 2.406550906569824e8]
 [1.8106913462789607e8, 2.6677768786796018e8]
 [1.9550310733949912e8, 2.918585907964363e8]
 [2.0990519914835802e8, 3.163868218665787e8]
 [2.242656374570966e8, 3.404770774339893e8]
 [2.3858562368294787e8, 3.64229193591574e8]
 [2.528646165837985e8, 3.877143171737028e8]
 [2.671037425800242e8, 4.109871581444607e8]
 [2.8130534605450904e8, 4.340902109413748e8]
 [2.954725300760899e8, 4.5705676507673633e8]
 [3.0960874760858804e8, 4.799130461124946e8]
 [3.2371751558658475e8, 5.026798229900373e8]
 [3.32893530726487e8, 5.174678778455995e8]

julia> Σ = getindex.(soln.u, 1) + getindex.(soln.u, 2)
32-element Vector{Float64}:
   2000.0
   2646.9401374590398
   4507.423877607305
   8725.066554854144
  18478.651841192805
  43423.059939864754
 111363.4221657838
 310632.3858155869
 929666.9584451192
      2.929060209255389e6
      9.301251946274448e6
      2.6209285958714496e7
      5.278380612094469e7
      9.700937449737367e7
      1.4183976965023756e8
      2.0694852943526444e8
      2.6487401358620068e8
      3.180841004305602e8
      3.6446832783648384e8
      4.070843841695659e8
      4.478468224958563e8
      4.873616981359354e8
      5.262920210149367e8
      5.647427148910859e8
      6.028148172745218e8
      6.405789337575012e8
      6.780909007244849e8
      7.153955569958838e8
      7.525292951528263e8
      7.895217937210827e8
      8.26397338576622e8
      8.503614085720865e8

julia> eco = getindex.(soln.u, 1)/Σ
32×32 Matrix{Float64}:
 3.3779e-13 4.47055e-13 7.61281e-13 1.47362e-12 … 1.27098e-7 1.33346e-7 1.39574e-7 1.43622e-7
 4.61982e-13 6.11419e-13 1.04117e-12 2.01541e-12 1.73827e-7 1.82372e-7 1.9089e-7 1.96426e-7
 8.34503e-13 1.10444e-12 1.88073e-12 3.64055e-12 3.13994e-7 3.29429e-7 3.44816e-7 3.54815e-7
 1.72761e-12 2.28644e-12 3.89353e-12 7.53676e-12 6.50039e-7 6.81993e-7 7.13846e-7 7.34546e-7
 3.91877e-12 5.18638e-12 8.83178e-12 1.70958e-11 1.4745e-6 1.54698e-6 1.61923e-6 1.66619e-6
 9.86431e-12 1.30551e-11 2.22313e-11 4.30334e-11 … 3.71159e-6 3.89405e-6 4.07592e-6 4.19412e-6
 2.70018e-11 3.57361e-11 6.08544e-11 1.17796e-10 1.01598e-5 1.06593e-5 1.11571e-5 1.14807e-5
 7.99297e-11 1.05785e-10 1.80139e-10 3.48696e-10 3.00747e-5 3.15531e-5 3.30269e-5 3.39846e-5
 2.51783e-10 3.33228e-10 5.67447e-10 1.09841e-9 9.47372e-5 9.93942e-5 0.000104037 0.000107053
 8.25473e-10 1.09249e-9 1.86038e-9 3.60115e-9 0.000310596 0.000325865 0.000341084 0.000350975
 2.67612e-9 3.54176e-9 6.0312e-9 1.16747e-8 … 0.00100693 0.00105643 0.00110577 0.00113783
 7.39009e-9 9.78056e-9 1.66551e-8 3.22395e-8 0.00278063 0.00291732 0.00305357 0.00314212
 1.37143e-8 1.81505e-8 3.09081e-8 5.98292e-8 0.00516021 0.00541388 0.00566674 0.00583106
 2.12058e-8 2.80652e-8 4.77917e-8 9.25109e-8 0.00797898 0.00837121 0.00876219 0.00901628
 2.67405e-8 3.53903e-8 6.02655e-8 1.16656e-7 0.0100615 0.0105561 0.0110492 0.0113696
 3.37435e-8 4.46585e-8 7.60481e-8 1.47207e-7 … 0.0126965 0.0133206 0.0139428 0.0143471
 3.99248e-8 5.28393e-8 8.99791e-8 1.74173e-7 0.0150223 0.0157608 0.0164969 0.0169753
 4.5822e-8 6.06441e-8 1.0327e-7 1.999e-7 0.0172412 0.0180887 0.0189336 0.0194826
 5.1163e-8 6.77127e-8 1.15307e-7 2.232e-7 0.0192508 0.0201972 0.0211405 0.0217535
 5.62181e-8 7.4403e-8 1.26699e-7 2.45253e-7 0.0211529 0.0221927 0.0232293 0.0239029
 6.11633e-8 8.09478e-8 1.37845e-7 2.66827e-7 … 0.0230136 0.0241449 0.0252726 0.0260055
 6.6039e-8 8.74006e-8 1.48833e-7 2.88097e-7 0.0248481 0.0260696 0.0272872 0.0280785
 7.09039e-8 9.38391e-8 1.59797e-7 3.0932e-7 0.0266786 0.0279901 0.0292974 0.030147
 7.57547e-8 1.00259e-7 1.70729e-7 3.30482e-7 0.0285038 0.029905 0.0313017 0.0322094
 8.05918e-8 1.06661e-7 1.81631e-7 3.51584e-7 0.0303239 0.0318145 0.0333004 0.0342661
 8.54151e-8 1.13044e-7 1.92501e-7 3.72626e-7 … 0.0321387 0.0337185 0.0352934 0.0363169
 9.0225e-8 1.1941e-7 2.03341e-7 3.93609e-7 0.0339485 0.0356173 0.0372808 0.0383619
 9.50221e-8 1.25759e-7 2.14152e-7 4.14537e-7 0.0357535 0.037511 0.039263 0.0404016
 9.98076e-8 1.32092e-7 2.24938e-7 4.35414e-7 0.0375541 0.0394002 0.0412404 0.0424363
 1.04583e-7 1.38412e-7 2.35699e-7 4.56246e-7 0.0393508 0.0412852 0.0432134 0.0444666
 1.09349e-7 1.44719e-7 2.4644e-7 4.77037e-7 … 0.041144 0.0431665 0.0451827 0.0464929
 1.12448e-7 1.48822e-7 2.53426e-7 4.90558e-7 0.0423102 0.0443901 0.0464634 0.0478108

```

Even if I use this:

```julia
julia> eco = getindex.(soln.u, 1)
32-element Vector{Float64}:
   1000.0
   1367.6600181427432
   2470.4796891385
   5114.450555250719
  11601.210411658707
  29202.513110268377
  79936.7711904854
 236625.52371589997
 745384.4693280015
      2.4437472687509363e6
      7.922433143990216e6
      2.1877766766866736e7
      4.060014627282435e7
      6.2777968383830175e7
      7.916321639200333e7
      9.989493619123092e7
      1.1819430058769086e8
      1.356524642999164e8
      1.5146396530996737e8
      1.6642929351258355e8
      1.8106913462789607e8
      1.9550310733949912e8
      2.0990519914835802e8
      2.242656374570966e8
      2.3858562368294787e8
      2.528646165837985e8
      2.671037425800242e8
      2.8130534605450904e8
      2.954725300760899e8
      3.0960874760858804e8
      3.2371751558658475e8
      3.32893530726487e8

julia> p_eco = eco/Σ
32×32 Matrix{Float64}:
 3.3779e-13 4.47055e-13 7.61281e-13 1.47362e-12 … 1.27098e-7 1.33346e-7 1.39574e-7 1.43622e-7
 4.61982e-13 6.11419e-13 1.04117e-12 2.01541e-12 1.73827e-7 1.82372e-7 1.9089e-7 1.96426e-7
 8.34503e-13 1.10444e-12 1.88073e-12 3.64055e-12 3.13994e-7 3.29429e-7 3.44816e-7 3.54815e-7
 1.72761e-12 2.28644e-12 3.89353e-12 7.53676e-12 6.50039e-7 6.81993e-7 7.13846e-7 7.34546e-7
 3.91877e-12 5.18638e-12 8.83178e-12 1.70958e-11 1.4745e-6 1.54698e-6 1.61923e-6 1.66619e-6
 9.86431e-12 1.30551e-11 2.22313e-11 4.30334e-11 … 3.71159e-6 3.89405e-6 4.07592e-6 4.19412e-6
 2.70018e-11 3.57361e-11 6.08544e-11 1.17796e-10 1.01598e-5 1.06593e-5 1.11571e-5 1.14807e-5
 7.99297e-11 1.05785e-10 1.80139e-10 3.48696e-10 3.00747e-5 3.15531e-5 3.30269e-5 3.39846e-5
 2.51783e-10 3.33228e-10 5.67447e-10 1.09841e-9 9.47372e-5 9.93942e-5 0.000104037 0.000107053
 8.25473e-10 1.09249e-9 1.86038e-9 3.60115e-9 0.000310596 0.000325865 0.000341084 0.000350975
 2.67612e-9 3.54176e-9 6.0312e-9 1.16747e-8 … 0.00100693 0.00105643 0.00110577 0.00113783
 7.39009e-9 9.78056e-9 1.66551e-8 3.22395e-8 0.00278063 0.00291732 0.00305357 0.00314212
 1.37143e-8 1.81505e-8 3.09081e-8 5.98292e-8 0.00516021 0.00541388 0.00566674 0.00583106
 2.12058e-8 2.80652e-8 4.77917e-8 9.25109e-8 0.00797898 0.00837121 0.00876219 0.00901628
 2.67405e-8 3.53903e-8 6.02655e-8 1.16656e-7 0.0100615 0.0105561 0.0110492 0.0113696
 3.37435e-8 4.46585e-8 7.60481e-8 1.47207e-7 … 0.0126965 0.0133206 0.0139428 0.0143471
 3.99248e-8 5.28393e-8 8.99791e-8 1.74173e-7 0.0150223 0.0157608 0.0164969 0.0169753
 4.5822e-8 6.06441e-8 1.0327e-7 1.999e-7 0.0172412 0.0180887 0.0189336 0.0194826
 5.1163e-8 6.77127e-8 1.15307e-7 2.232e-7 0.0192508 0.0201972 0.0211405 0.0217535
 5.62181e-8 7.4403e-8 1.26699e-7 2.45253e-7 0.0211529 0.0221927 0.0232293 0.0239029
 6.11633e-8 8.09478e-8 1.37845e-7 2.66827e-7 … 0.0230136 0.0241449 0.0252726 0.0260055
 6.6039e-8 8.74006e-8 1.48833e-7 2.88097e-7 0.0248481 0.0260696 0.0272872 0.0280785
 7.09039e-8 9.38391e-8 1.59797e-7 3.0932e-7 0.0266786 0.0279901 0.0292974 0.030147
 7.57547e-8 1.00259e-7 1.70729e-7 3.30482e-7 0.0285038 0.029905 0.0313017 0.0322094
 8.05918e-8 1.06661e-7 1.81631e-7 3.51584e-7 0.0303239 0.0318145 0.0333004 0.0342661
 8.54151e-8 1.13044e-7 1.92501e-7 3.72626e-7 … 0.0321387 0.0337185 0.0352934 0.0363169
 9.0225e-8 1.1941e-7 2.03341e-7 3.93609e-7 0.0339485 0.0356173 0.0372808 0.0383619
 9.50221e-8 1.25759e-7 2.14152e-7 4.14537e-7 0.0357535 0.037511 0.039263 0.0404016
 9.98076e-8 1.32092e-7 2.24938e-7 4.35414e-7 0.0375541 0.0394002 0.0412404 0.0424363
 1.04583e-7 1.38412e-7 2.35699e-7 4.56246e-7 0.0393508 0.0412852 0.0432134 0.0444666
 1.09349e-7 1.44719e-7 2.4644e-7 4.77037e-7 … 0.041144 0.0431665 0.0451827 0.0464929
 1.12448e-7 1.48822e-7 2.53426e-7 4.90558e-7 0.0423102 0.0443901 0.0464634 0.0478108

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [June 2, 2021, 1:53pm UTC](https://discourse.julialang.org/t/how-to-divide-vector-elements-in-julia/62274/2 "2021-06-02T13:53:00Z")

</div>

You are just missing a `.` before the division (`./`). It should be `eco = getindex.(soln.u, 1)./Σ` to obtain elementwise division rather than matrix division.

See the simplified example below:

```julia
julia> u = [[1,2],[3,4],[5,6]]
3-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]
 [5, 6]

julia> ∑ = sum.(u)
3-element Vector{Int64}:
  3
  7
 11

julia> percent = u./∑
3-element Vector{Vector{Float64}}:
 [0.3333333333333333, 0.6666666666666666]
 [0.42857142857142855, 0.5714285714285714]
 [0.45454545454545453, 0.5454545454545454]

julia> eco = getindex.(percent, 1)
3-element Vector{Float64}:
 0.3333333333333333
 0.42857142857142855
 0.45454545454545453

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [June 2, 2021, 4:50pm UTC](https://discourse.julialang.org/t/how-to-divide-vector-elements-in-julia/62274/3 "2021-06-02T16:50:42Z")

</div>

Another option is to use map.

```julia
julia> u = [[1,2],[3,4],[5,6]]
3-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]
 [5, 6]

julia> Σ = map(sum,u)
3-element Vector{Int64}:
  3
  7
 11

julia> percent = map(./,u,Σ)
3-element Vector{Vector{Float64}}:
 [0.3333333333333333, 0.6666666666666666]
 [0.42857142857142855, 0.5714285714285714]
 [0.45454545454545453, 0.5454545454545454]

julia> eco = map(first,percent)
3-element Vector{Float64}:
 0.3333333333333333
 0.42857142857142855
 0.45454545454545453

```
