# How to convert a Matrix{Array{Int64, 0}} to a Matrix{Int64}

**URL:** https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136
**Category:** General Usage
**Tags:** question
**Created:** [October 18, 2023, 8:00pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136 "2023-10-18T20:00:24Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [October 18, 2023, 8:00pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/1 "2023-10-18T20:00:24Z")

</div>

A function returns a matrix `nr`, like this:

```julia
3×3 Matrix{Array{Int64, 0}}:
 fill(4) fill(2) fill(1)
 fill(1) fill(2) fill(2)
 fill(2) fill(1) fill(1)

```

`fill(4)[]` is displayed as 4.  
How to get from the matrix `nr` the matrix:

```julia
[ 4 2 1;
  1 2 2;
  2 1 1]

```

in a more compact way than:

```julia
Nr= reshape([nr[i][] for i in eachindex(nr)], 3,3)

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 18, 2023, 8:10pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/2 "2023-10-18T20:10:21Z")

</div>

Does this work for you?

```julia
julia> a = [ Ref(1) Ref(2)
             Ref(3) Ref(4) ]
2×2 Matrix{Base.RefValue{Int64}}:
 RefValue{Int64}(1) RefValue{Int64}(2)
 RefValue{Int64}(3) RefValue{Int64}(4)

julia> getfield.(a,1)
2×2 Matrix{Int64}:
 1 2
 3 4

```

(I’m not sure how to build your example)

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [October 18, 2023, 8:22pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/3 "2023-10-18T20:22:43Z")

</div>

1. In any case, you can `map` over your array to avoid explicit reshaping:

```julia
julia> A0 = reshape([fill(i) for i = 1:4], 2, 2)
2×2 Matrix{Array{Int64, 0}}:
fill(1) fill(3)
fill(2) fill(4)

julia> map(x -> x[], A0)
2×2 Matrix{Int64}:
1 3
2 4

```

2. `fill(4)` is indeed a zero-dimensional array, i.e., `typeof(fill(4)) == Array{Int64, 0}` and can be accessed as `fill(4)[]`, i.e., without giving any index. The corresponding functional form is `getindex(fill(4))` which can be broadcasted:

```julia
julia> getindex.(A0)
2×2 Matrix{Int64}:
1 3
2 4

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 18, 2023, 8:26pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/4 "2023-10-18T20:26:43Z")

</div>

```julia
julia> a = [fill(a+b) for a in 1:3, b in 1:3]
3×3 Matrix{Array{Int64, 0}}:
 fill(2) fill(3) fill(4)
 fill(3) fill(4) fill(5)
 fill(4) fill(5) fill(6)

julia> only.(a)
3×3 Matrix{Int64}:
 2 3 4
 3 4 5
 4 5 6

```

`only(x)` is the same as `x[]` which is the same as `getindex(x)` I believe

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [October 18, 2023, 8:31pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/5 "2023-10-18T20:31:31Z")

</div>

Great, thank you!!!

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [October 18, 2023, 8:33pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/6 "2023-10-18T20:33:05Z")

</div>

In this case yes, i.e., `@less only(fill(4))` shows that

```julia
# @ Base.Iterators iterators.jl:1534
only(a::AbstractArray{<:Any, 0}) = @inbounds return a[]

```

---

<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 18, 2023, 8:43pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/7 "2023-10-18T20:43:42Z")

</div>

> [@empet](#):
>
> A function returns a matrix `nr`, like this:

(A more basic question is why your function is returning an array of 0-dimensional arrays. That sounds like it could be a usage mistake that you might want to correct, rather than fixing up the output.)

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [October 18, 2023, 8:45pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/8 "2023-10-18T20:45:32Z")

</div>

I would use `stack(array)` or `only.(array)`. But I’ll echo others’ thoughts that perhaps one could avoid creating this unusual matrix representation in the first place…

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [October 18, 2023, 8:46pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/9 "2023-10-18T20:46:59Z")

</div>

That odd array is returned by this code:

```julia
import Polynomials: Polynomial, derivative
import StatsBase: sample

function nroots!(p, z;eps=1e-05, maxiter=50)
   dp= derivative(p)
   ddp=derivative(dp)
   prod=ddp*p 
   nr=zeros(Int, size(z)) 
   for _ in 1:maxiter 
       z, zprev= z-p(z)/(dp(z) - prod(z)/(2*dp(z))), z
       nr[findall(x->x^2>eps, abs(z-zprev))] .+= 1
   end 
   return nr 
end 
x= 0:0.05:1
z= x' .+ x*im
p = Polynomial(sample([1.0, -1.0, 1im, -1im], 5))
nr=nroots!.(p, z)

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 18, 2023, 10:01pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/10 "2023-10-18T22:01:37Z")

</div>

Side note, is this a bug? Why in the matrix case the return is an Int matrix? (with a generator it is not)

```julia
julia> a = [fill(1), fill(2)]
2-element Vector{Array{Int64, 0}}:
 fill(1)
 fill(2)

julia> a = [fill(1) fill(2)]
1×2 Matrix{Int64}:
 1 2

```

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [October 18, 2023, 11:01pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/11 "2023-10-18T23:01:57Z")

</div>

Don’t think that is a bug, but the same as observed [here](https://discourse.julialang.org/t/how-not-to-concatenate-in-hvcat/104146), i.e., `[x, y]` lowers to `vect(x, y)` whereas `[x y]` lowers to `hcat(x, y)`.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 19, 2023, 6:01am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-array-int64-0-to-a-matrix-int64/105136/12 "2023-10-19T06:01:23Z")

</div>

For completeness this is the corresponding vector concatenation:

```julia
julia> a = [fill(1); fill(2)]
2-element Vector{Int64}:
 1
 2

```

There is no higher-dimensional non-concatenating constructor, but to some extent it can be worked around by wrapping the elements in one-element containers and concatenate those, e.g. the highly contrived

```julia
julia> a = [fill(fill(1)) fill(fill(2))]
1×2 Matrix{Array{Int64, 0}}:
 fill(1) fill(2)

```
