How to create a 2x1 Array?

TL;DR, answer: to get a 2x1 Array/Matrix transpose a 1x2 matrix, for example: [2 4]'
A more general solution that also works for complex numbers in Julia V0.7- would be transpose([2 4])


I wanted to test

julia> maximum([1 2; 3 4], 2)
2×1 Array{Int64,2}:
 2
 4

So I tried

julia> maximum([1 2; 3 4], 2) == [2;4]
false

I expected the value to be true, but the value is false because

julia>  [2;4]
2-element Array{Int64,1}:
 2
 4

That is, a 2×1 Array{Int64,2} is not the same as a 2-element Array{Int64,1} (2D vs 1D)

However,

julia> [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

So [2;4] (note the semicolon) seems the proper way to get a 2×1 Array{Int64,2} 2-dimensional array.

What am I missing?

try to use all(a.==b) instead of a==b. Don’t have a computer atm but maybe == is comparing the references?

.== is element-wise comparison

julia> all(maximum([1 2; 3 4], 2) .== [2;4])
true

That works, but it doesn’t answer the question how to create a 2×1 Array{Int64,2} 2-dimensional array.

Why not just Matrix{Int64}(2, 1)?

If you want it initialized, you can do zeros(Int64, 2, 1)

Yes, that can be done, but it requires multiple statements. But why should the simplest case require the most work? [1 2; 3 4] gives me a 2x2 matrix. [1 2] gives me a 1x2 matrix. Why doesn’t there seem to be a simple way to create a 2x1 matrix? It turns out that [1;2] is the same as [1,2]. One would think/hope/expect that [1;2] would result in a 2x1 matrix, but it doesn’t.

1 Like

You didn’t specify that you wanted specific values in your matrix, so I didn’t provide you with that. hcat(1,2)' will do what you want (if you want 1 and 2 as your elements. Substitute as appropriate.)

The problem with hcat(1,2)' is that its type is Adjoint{Int64,Array{Int64,2}}, which is correct if you do not use complex numbers but it is simpler to write hvcat(1, 1, 2) I guess to get 2×1 Array{Int64,2} directly.

The problem with hcat(1,2)' is that its type is Adjoint{Int64,Array{Int64,2}}

julia> typeof(hcat(1,2)')
Array{Int64,2}

Perhaps in 0.7? I’m using -release.

julia> hcat(2,4)
1×2 Array{Int64,2}:
 2  4

But I’m looking for a 2x1 matrix, not a 1x2 matrix. So I tried

julia> hvcat((1,1),2,4)
2×1 Array{Int64,2}:
 2
 4

and indeed

julia> maximum([1 2; 3 4], 2) == hvcat((1,1),2,4)
true

We have a winner! Thank you all for your suggestions.

You’ll note that I transposed the hcat - see the ' at the end?

julia> maximum([1 2; 3 4], 2) == hcat(2,4)'
true

Very clever. I like hcat(2,4)’ even better than hvcat((1,1),2,4). Thanks.

In 0.7 I use reshape([2,4], Val{2}())
I think it is quiet nice.
It works for any length array, giving a column matrix.

Not available in 0.6 though.

In 0.5 (and earlier) I used to use [2,4]'' but that doesn’t work in 0.6 or 0.7
because we now take vector transpose seriously.

See also https://github.com/JuliaLang/julia/pull/23790

I use

reshape([1, 2], :, 1)

which works in both v0.6.2 and v0.7-.

2 Likes

Why use hcat(2,4)' instead of [2 4]'? The latter seems terser, nicer and more idiomatic.

Yes - on 0.7.

Also, for this particular use case (testing) you can also

squeeze(maximum([1 2; 3 4], 2), 2) == [2, 4]

or the slightly less elegant (because it does not catch some shape errors)

vec(maximum([1 2; 3 4], 2)) == [2, 4]
1 Like

Shouldn’t it be [1 2].' for a general solution, otherwise complex values will be an issue.

Note that the .' is deprecated in v0.7-.

copy(transpose([1 2])) will be fine though.

Ooops, thanks for the clarification and pointing out that transpose is the underlying function to call, not ctranspose/adjoint.

It is probably some kind of stockholm syndrom when we develop psychological alliance with pseudo-solutions like reshape, hcat, transpose, etc.