# Indexing into Matrix

**URL:** https://discourse.julialang.org/t/indexing-into-matrix/45806
**Category:** General Usage
**Created:** [August 30, 2020, 10:00pm UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806 "2020-08-30T22:00:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![grunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grunst/32/9271_2.png) [@grunst](https://discourse.julialang.org/u/grunst)
#### Post date: [August 30, 2020, 10:00pm UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/1 "2020-08-30T22:00:24Z")

</div>

Is there a github issue or discourse post that discusses why

```julia
A[1,:]

```

returns a column vector instead of a row vector (`Adjoint`) for a matrix A?

From the pattern

`A[[1],:]` return a `1xn` matrix

`A[:,[1]]` return a `nx1` matrix

`A[:,1]` return a column vector

I was expecting `A[1,:]` to return a row vector.

---

<div class="post-metadata">

### Author: ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)
#### Post date: [August 30, 2020, 10:22pm UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/2 "2020-08-30T22:22:50Z")

</div>

Both `A[1, :]` and `A[:, 1]` are `Vector`s. There is no difference between column vectors and row vectors.

This generalizes to multidimensional arrays, where all indices but one are scalars (e.g. `A[1, 2, :, 4]` returns a `Vector`). So, it seems strange treating the specific situation `A[i, :]` any differently (in case you think it should return a `Transpose`, for example).

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 30, 2020, 10:23pm UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/3 "2020-08-30T22:23:34Z")

</div>

If you want a more in-depth explanation, [this is related to an old post of mine](https://discourse.julialang.org/t/eachrow-over-array-any-2-does-not-return-rows-why/34601).

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [August 30, 2020, 11:27pm UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/4 "2020-08-30T23:27:53Z")

</div>

What you refer to as a _row vector_ is known in Julia as a `Matrix` or a 2D `Array` with only one row, so if you want that definition, use the `A[1:1,:]` notation.

```julia
julia> A = rand(3,3)
3×3 Array{Float64,2}:
 0.0979413 0.491558 0.134096
 0.991139 0.0406479 0.441085
 0.382994 0.419503 0.272454

julia> A[1:1,:]
1×3 Array{Float64,2}:
 0.0979413 0.491558 0.134096

```

---

<div class="post-metadata">

### Author: ![bengtjo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bengtjo/32/15992_2.png) [@bengtjo](https://discourse.julialang.org/u/bengtjo)
#### Post date: [August 31, 2020, 12:27am UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/5 "2020-08-31T00:27:30Z")

</div>

It may be that [issue 4774](https://github.com/JuliaLang/julia/issues/4774) and the JuliaCon talk [“Taking vector transposes seriously”](https://www.youtube.com/watch?v=C2RO34b_oPM) may give some historical context and useful links.

---

<div class="post-metadata">

### Author: ![grunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grunst/32/9271_2.png) [@grunst](https://discourse.julialang.org/u/grunst)
#### Post date: [August 31, 2020, 8:31am UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/6 "2020-08-31T08:31:26Z")

</div>

> [@Seif\_Shebl](#):
>
> What you refer to as a _row vector_ is known in Julia as a `Matrix` or a 2D `Array` with only one row,

To be more specific. One of the outcomes of the 4774 issue was the need to have true row vectors (now called `Adjoint` in Julia) that are different from `1xn` matrices to have e.g. inner products `a'*b` return scalars. Since julia has these “true” row vectors and indexing like `A[:,1]` returns a column vector (instead of a `nx1` matrix) I was wondering if this could work

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

julia> A[1,:]
1×2 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 1 2

```

---

<div class="post-metadata">

### Author: ![grunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grunst/32/9271_2.png) [@grunst](https://discourse.julialang.org/u/grunst)
#### Post date: [August 31, 2020, 9:10am UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/7 "2020-08-31T09:10:23Z")

</div>

I see your point about multidimensional arrays as `Array` in base julia has no concept of up or down indices. i.e. a distinction between tensors `A^{ij}_{kl}` and `A^{ijk}_l` so there is no way to tell whether `A[1,2,:,4]` should return a row or column vector but for the 2D case the convention `A^i_j` seems useful.

---

<div class="post-metadata">

### Author: ![grunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grunst/32/9271_2.png) [@grunst](https://discourse.julialang.org/u/grunst)
#### Post date: [August 31, 2020, 10:31am UTC](https://discourse.julialang.org/t/indexing-into-matrix/45806/8 "2020-08-31T10:31:40Z")

</div>

Thanks for the link. That post is indeed closely related.
