# Arrays to vector conversion

**URL:** <https://discourse.julialang.org/t/arrays-to-vector-conversion/89513>\
**Category:** New to Julia\
**Tags:** arrays\
**Created:** [October 30, 2022, 5:27am UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513 "2022-10-30T05:27:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 30, 2022, 5:27am UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513/1 "2022-10-30T05:27:56Z")

</div>

Hello all,

I am trying to write a function where i have to compare two quantities, the simplified representation of the problem where i am stuck is as

I get these type of two quantities as follows:

```julia
a = [[9], [8]]
b = [9.0121, 9.008]

if isapprox(a, b; atol=1e-5)
   print ("done")
else
  print("not done")
end

```

I am not able to compare “a” and “b” and it is giving me this error ` DimensionMismatch: dimensions must match: a has dims (Base.OneTo(0),), b has dims (Base.OneTo(2),), mismatch at 1`

I guess i have to convert “b” such that its shape is similar to “a”, any idea how can i do that? thank you

---

<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:** [October 30, 2022, 7:59am UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513/2 "2022-10-30T07:59:43Z")

</div>

You could use: `reduce(vcat,a)`, to convert `a` to a vector like `b`, which seems simpler than the other way around.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 30, 2022, 8:57am UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513/3 "2022-10-30T08:57:36Z")

</div>

If I run your code I don’t get a `DimensionMismatch` error. Instead I get `ERROR: MethodError: no method matching -(::Vector{Int64}, ::Float64)`.

It’s not just a problem of different shapes, but of very different types. Note that `[[9], [8]]` In Julia is not a matrix with two rows like in Numpy. It’s a vector that contains two vectors. If that’s really what you want, and if you know that all the vectors in `a` have one element, you can do `isapprox(only.(a), b; atol=1e-5)`. Here `only.(a)` will collect the single elements of `[9]` and `[8]` into a new vector `[9, 8]`.

---

<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:** [October 30, 2022, 9:09am UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513/4 "2022-10-30T09:09:13Z")

</div>

Something like

```julia
all(isapprox(x, y, tol) for (x, y) in zip(flatten(a), flatten(b))))

```

`flatten` is from `Iterators`, and is the key. It will unroll nested arrays to any depth, spitting out scalars, without allocating intermediate arrays.

Warning: untested and written on a phone.

---

<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:** [October 30, 2022, 9:58am UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513/5 "2022-10-30T09:58:07Z")

</div>

A merge of @sijo and @DNF solutions, that is short and non-allocating:

```julia
all(isapprox(only(x), y; atol=1e-5) for (x,y) in zip(a,b))

```

However, these non-allocating approaches do not seem to do exactly the same as OP, because for vector inputs a vector norm is used.

---

<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:** [October 30, 2022, 12:14pm UTC](https://discourse.julialang.org/t/arrays-to-vector-conversion/89513/6 "2022-10-30T12:14:27Z")

</div>

> [@DNF](#):
>
> Something like
> 
> ```julia
> all(isapprox(x, y, tol) for (x, y) in zip(flatten(a), flatten(b)))
> 
> ```

> [@rafael.guerra](#):
>
> `all(isapprox(only(x), y; atol=1e-5) for (x,y) in zip(a,b))`

Using `isapprox` elementwise is often a bad idea. For example:

```julia
julia> isapprox([1, 0], [1, 1e-16])
true

julia> all(isapprox.([1, 0], [1, 1e-16]))
false

```

`1e-16 ≈ 0` is false, and rightly so, because the two numbers have no significant digits in common, and there is no reference by which to set an absolute scale. (You don’t know the “units”. See also the [`isapprox` documentation](https://docs.julialang.org/en/v1/base/math/#Base.isapprox) on why the default tolerance is _relative_ error, not _absolute_ error.)

The advantage of applying `isapprox` to the whole array at once is that the norm of the whole array sets an overall scale with which to compare the elements: `1e-16 - 0` _is_ small compared to the norm `≈ 1`, so `[1,0] ≈ [1,1e-16]` rightly returns `true`.

Approximate comparison is one of those things that’s really easy to get wrong, unfortunately. (e.g. `numpy.isclose` got it wrong.)

PS. I think “non-allocating” here is probably an irrelevant metric. `isapprox` is not something that you usually do in performance-critical settings.
