# Unintuitive Julia result: selecting a row from a matrix

**URL:** <https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222>\
**Category:** New to Julia\
**Created:** [August 11, 2021, 7:25pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222 "2021-08-11T19:25:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Blais](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_blais/32/28085_2.png) [@Brian\_Blais](https://discourse.julialang.org/u/Brian_Blais)\
**Post date:** [August 11, 2021, 7:25pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/1 "2021-08-11T19:25:28Z")

</div>

I bumped into this example trying to convert some julia to python and was surprised. In the example here, python and matlab give the same answer, while julia differs…

If you start with…  
`X=rand(3,100)` (and do `X=mat(X)` if in python)

and then do the following…

matlab/octave: `X(1,:)*X(1,:)'`  
julia: `X[1,:]*X[1,:]'`  
python: `X[1,:]*X[1,:].T`

what size should the result be and why? in python and matlab (and julia), `X[1,:]` will select a row. python and matlab however return a row vector (or a 1xN matrix) while julia returns a column vector (Nx1). there may be a reason, but I am not sure what that is. Is this expected?

---

<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:** [August 11, 2021, 7:26pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/2 "2021-08-11T19:26:44Z")

</div>

> [@Why column major?](https://discourse.julialang.org/t/why-column-major/24374/4):
>
> As I understand it, it was mainly historical precedent, driven by the fact that most linear-algebra libraries (LAPACK etc.) are column-major (because Fortran is column-major, and hence Matlab is column-major), though it would have been possible to work around this with judicious use of transposition. It’s been discussed before, e.g. [https://groups.google.com/d/msg/julia-users/GkvDunqE0ww/2gIDmA5cAAAJ](https://groups.google.com/d/msg/julia-users/GkvDunqE0ww/2gIDmA5cAAAJ)

```julia
julia> x[1,:] # this returns a Vector, which is a column

julia> x[1,:]' # this traspose the Vector into a row-vector

```

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [August 11, 2021, 7:34pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/3 "2021-08-11T19:34:27Z")

</div>

Yes, it’s expected. `X[1,:]` gives a flat vector. In case you want an `1xn` array, do `X[1:1,:]`.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 11, 2021, 7:35pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/4 "2021-08-11T19:35:28Z")

</div>

Yup, there absolutely is a reason and it’s a doozy. It turns out that the embedding of one-dimensional containers into the algebra of matrices is not obvious. There are a number of choices you must make in how they behave, impacting everything from indexing to transposes. It’s one of the few design decisions that has had so much thought put into it that many Julia folk who have been around for a while have it memorized ([#4774](https://github.com/JuliaLang/julia/issues/4774)). It’s spawned a paper (still a draft) and [presentations](https://www.youtube.com/watch?v=C2RO34b_oPM). There’s even an `:4774:` emoji :4774: commemorating its author and major thought-leader here and on Slack.

The end-result, though, is a remarkably simple rule: The resulting dimensionality of an indexing expression is the sum of the dimensionalities of the indices. Or even more specifically, the resulting axes of an indexing expression is the concatenation of all the axes of all the indices used.

So that’s why `X[1, :]` is a 1-dimensional vector while `X[[1], :]` is a `1xN` row matrix.

---

<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:** [August 11, 2021, 7:46pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/5 "2021-08-11T19:46:28Z")

</div>

> [@mbauman](#):
>
> The resulting dimensionality of an indexing expression is the sum of the dimensionalities of the indices. Or even more specifically, the resulting axes of an indexing expression is the concatenation of all the axes of all the indices used.

I hope this is in a very visible place in docs and I just happened to miss it 😑

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 11, 2021, 7:55pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/6 "2021-08-11T19:55:34Z")

</div>

It appears here: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing), which is where we talk about all things indexing.

---

<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:** [August 11, 2021, 8:02pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/7 "2021-08-11T20:02:15Z")

</div>

no but your summary here can beat 3 paragraphs and lengthy example in the actual manual I think.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 11, 2021, 9:06pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/8 "2021-08-11T21:06:11Z")

</div>

> [@Brian\_Blais](#):
>
> while julia returns a column vector (Nx1)

One thing to keep in mind, that may make this less weird, is that Julia does _not_ return an Nx1 array, but simply an N vector (no ‘x1’). The distinction makes a difference.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 11, 2021, 9:29pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/9 "2021-08-11T21:29:49Z")

</div>

> [@Brian\_Blais](#):
>
> matlab/octave: `X(1,:)*X(1,:)'`  
> julia: `X[1,:]*X[1,:]'`  
> python: `X[1,:]*X[1,:].T`
> 
> what size should the result be and why? in python and matlab (and julia), `X[1,:]` will select a row. python and matlab however return a row vector (or a 1xN matrix)

This is actually not quite right. First of all, numpy does not return a row vector, but a flat vector like Julia:

```julia
In [21]: X[1,:].shape
Out[21]: (5,)

```

It _prints_ like a row, but it’s not.

Also, you should note that Matlab and numpy disagree with each other (as well as with Julia) in other ways:

Numpy:

- ‘row slice’ produces a _flat_ vector
- transposing a flat vector does _nothing_, the vector is unchanged
- multiplication with `*` is element-by-element and broadcasting, _not_ a linear algebra operation
- =\> `X[1,:]*X[1,:].T` returns an N _vector_

Matlab

- ‘row slice’ produces a 1xN _matrix_, not a vector
- transposing it creates an Nx1 matrix
- multiplication with `*` is a linear algebra operation
- =\> `X(1,:)*X(1,:)'` returns a 1x1 matrix (‘scalar’-like)

Julia:

- ‘row slice’ produces a flat vector
- transposing it produces a special transpose (or in the case of `'` an _adjoint_) vector, which is ‘row-like’
- multiplication with `*` is a linear algebra operation
- =\> `X[1,:]*X[1,:]'` returns an NxN matrix

So it’s not like Julia is the odd one out here. They all disagree.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 11, 2021, 9:48pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/10 "2021-08-11T21:48:00Z")

</div>

> [@Brian\_Blais](#):
>
> (and do `X=mat(X)` if in python)

I noticed this just now, and it’s not something I’ve seen before. Does it alter the behavior of numpy arrays?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 11, 2021, 10:20pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/11 "2021-08-11T22:20:48Z")

</div>

Yes, we need to pester @jiahao to make his draft paper public; if I remember right he details the “universes” of reasonable ways you can get vectors to work with matrices. None are perfect.

---

<div class="post-metadata">

**Author:** ![Brian\_Blais](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_blais/32/28085_2.png) [@Brian\_Blais](https://discourse.julialang.org/u/Brian_Blais)\
**Post date:** [August 11, 2021, 11:04pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/12 "2021-08-11T23:04:36Z")

</div>

very interesting. I can see the reason. I caught me by surprise because of the difference with matlab and python with matrix operations. python uses two data types (array and matrix) so the operations for matrix are a bit different. matlab only had matrix operations. at least now I can understand the logic. thanks!

---

<div class="post-metadata">

**Author:** ![Brian\_Blais](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_blais/32/28085_2.png) [@Brian\_Blais](https://discourse.julialang.org/u/Brian_Blais)\
**Post date:** [August 11, 2021, 11:06pm UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/13 "2021-08-11T23:06:55Z")

</div>

> [@DNF](#):
>
> > [@Brian\_Blais](#):
> >
> > (and do `X=mat(X)` if in python)
> 
> I noticed this just now, and it’s not something I’ve seen before. Does it alter the behavior of numpy arrays?

yes it does. it make all operations 2d – so that you’re only doing matrix operations. in this way it behaves more like matlab, which is why it was introduced. you’re correct that for an array X[1,:] returns a flat array.

---

<div class="post-metadata">

**Author:** ![udax](https://avatars.discourse-cdn.com/v4/letter/u/a587f6/32.png) [@udax](https://discourse.julialang.org/u/udax)\
**Post date:** [February 24, 2023, 5:31am UTC](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222/14 "2023-02-24T05:31:46Z")

</div>

Just be careful here, `x[1,:]' ` will lead to a _conjugate_ transpose in case `x` is complex.
