Converting 1×10 Array{Int64,2} to 10x1 Array{Int64,2}

A = [0 0 0 0 0 0 0 0 0 0]

is a 1×10 Array{Int64,2}.

Why is A’ not a 10x1 Array{Int64,2} but a 10×1 Adjoint{Int64,Array{Int64,2}}?

Or, how do I convert A to a 10x1 Array{Int64,2}?

Because the adjoint is more efficient, you can collect it if you want to:

julia> a = collect(1:10)
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> a'
1×10 adjoint(::Vector{Int64}) with eltype Int64:
 1  2  3  4  5  6  7  8  9  10

julia> collect(a')
1×10 Matrix{Int64}:
 1  2  3  4  5  6  7  8  9  10

julia> using BenchmarkTools

julia> @btime $a';
  1.200 ns (0 allocations: 0 bytes)

julia> @btime collect($a');
  44.489 ns (1 allocation: 160 bytes)

But most of the time that shouldn’t actually be necessary as you can work with the adjoint.

2 Likes

you may be happier starting with (note the commas)
A = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

2 Likes

Or just: transpose(A)

Note that ' will also transpose complex values - it’s only a syntax shortcut for conjugate transpose after all. transpose(x) itself is recursive, so if you only want to change the dimensionality on the top level, use permutedims instead.

3 Likes

First of all, you can just use the adjoint, a', as is, in most cases you don’t need to collect it. If you absolutely need a 10x1 Array, then reshape(a, :, 1) will be more efficient than collect(a'). If what you actually need is a vector, and not a matrix, use vec(a).

2 Likes

If the type of A' depended on the number of columns in A, that would make things harder for the compiler (since the size of an Array can change at runtime).

In Julia, the answer to the question “how do I convert x to a T” is usually T(x). In your case:

Array{Int64,2}(A')

(In some rare cases, you have to be more explicit and do convert(T, x).)

1 Like

Array(A') also works.

2 Likes