# Confusing Results When Comparing Arrays in Julia

**URL:** <https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 22, 2024, 4:23am UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689 "2024-09-22T04:23:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaiyue](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@Kaiyue](https://discourse.julialang.org/u/Kaiyue)\
**Post date:** [September 22, 2024, 4:23am UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689/1 "2024-09-22T04:23:15Z")

</div>

Hi,

I’m using Julia version 1.10.5 (2024-08-27) on Ubuntu 22.04. I’ve been experimenting with comparing arrays, but I’m getting some confusing results. Here’s my code:

```julia
u = [1;2;3]
v = [1 2 3]
A = [1 2 3; 4 5 6]
print(A[1,:])
print(A[1,:] == u)
print(A[1,:] == v)

```

The output:

```julia
3-element Vector{Int64}:
 1
 2
 3

1×3 Matrix{Int64}:
 1 2 3

2×3 Matrix{Int64}:
 1 2 3
 4 5 6

[1, 2, 3]
true
false

```

I’m puzzled by the fact that `print(A[1, :] == u)` returns `true`, while `print(A[1, :] == v)` returns `false`. To my understanding, `A[1, :]` should be a row vector, and the output seems to confirm this.

Can anyone explain why this happens?

Many thanks!

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 22, 2024, 4:29am UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689/2 "2024-09-22T04:29:58Z")

</div>

I find it unintuitive, but what’s going on is

```julia
julia> A[1,:]
3-element Vector{Int64}:
 1
 2
 3

```

is a column vector and

```julia
julia> A[1:1, :]
1×3 Matrix{Int64}:
 1 2 3

```

is the row vector.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 22, 2024, 5:47am UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689/3 "2024-09-22T05:47:46Z")

</div>

> [@Kaiyue](#):
>
> To my understanding, `A[1, :]` should be a row vector, and the output seems to confirm this.

`Vector` doesn’t inherently distinguish row vs column vectors, you need `Matrix` for that. `Array`s are column-major so `Vector`s tend to be treated as column vectors, like in array literal syntax (`[1,2,3] == [1;2;3]`) and linear algebra (`A*u` works but `A*v` errors). You’re just seeing the difference between the `repr(...)` used by `print` and the `repr("text/plain", ...)` used by the REPL. Display is actually more complicated than that (`show`, `display`, `println`, etc) but that’s the short version.

```julia
julia> [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> repr([1, 2, 3])
"[1, 2, 3]"

julia> repr("text/plain", [1, 2, 3])
"3-element Vector{Int64}:\n 1\n 2\n 3"

```

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 22, 2024, 9:01am UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689/4 "2024-09-22T09:01:10Z")

</div>

This behaviour is documented:

> If all the indices are scalars, then the result `X` is a single element from the array `A`. Otherwise, `X` is an array with the same number of dimensions as the sum of the dimensionalities of all the indices.  
> [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing)

That is, if you index with a scalar, its dimensionality is zero. So `A[1, :]` has dimension 1. That is, it is a `Vector{Int}` which is an alias for `Array{Int, 1}`. Now, `u` is also a `Vector{Int}`, whereas `v` is a `Matrix{Int}` (which is an alias for `Array{Int, 2}`).

There is no concept of row or column vectors in julia. There are arrays of various sizes. The one-dimensional array is called a `Vector`, the two-dimensional is called a `Matrix`. In many linear algebra settings, a `Vector` is treated as a `n x 1` matrix, or a column vector. So that e.g. `collect(transpose(u))` is a 1 x 3 matrix.

---

<div class="post-metadata">

**Author:** ![Kaiyue](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@Kaiyue](https://discourse.julialang.org/u/Kaiyue)\
**Post date:** [September 22, 2024, 12:33pm UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689/6 "2024-09-22T12:33:38Z")

</div>

Hi Benny, thank you!

---

<div class="post-metadata">

**Author:** ![Kaiyue](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@Kaiyue](https://discourse.julialang.org/u/Kaiyue)\
**Post date:** [September 22, 2024, 12:34pm UTC](https://discourse.julialang.org/t/confusing-results-when-comparing-arrays-in-julia/119689/7 "2024-09-22T12:34:41Z")

</div>

Hi sgaure, thank you!
