# Sort and dims=1 for vector input

**URL:** https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212
**Category:** New to Julia
**Tags:** question
**Created:** [March 30, 2021, 9:42am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212 "2021-03-30T09:42:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ovt](https://avatars.discourse-cdn.com/v4/letter/o/87869e/32.png) [@ovt](https://discourse.julialang.org/u/ovt)
#### Post date: [March 30, 2021, 9:42am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/1 "2021-03-30T09:42:03Z")

</div>

Hi,

I don’t understand why a function like `sort` doesn’t accept dims=1 for a vector input. Let’s take `b=rand(3)` for instance. `sort(b)` works, but `sort(b,dims=1)` gives an error. It means that, in a case where `size(b,2)` is not fixed, I have to catch when `size(b,2)==1` in order to compute `sort(b)` instead of `sort(b,dims=1)`. Why doesn’t sort behave like that already?

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [March 30, 2021, 9:54am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/2 "2021-03-30T09:54:05Z")

</div>

Julia treats vectors as different than `n x 1` matrices. For example,

```julia
julia> x = rand(4, 1) # 4 x 1 matrix
4×1 Matrix{Float64}:
 0.5699429434936178
 0.38264907855330454
 0.2974011813965416
 0.9032771294647794

julia> sort(x; dims=1) # works fine
4×1 Matrix{Float64}:
 0.2974011813965416
 0.38264907855330454
 0.5699429434936178
 0.9032771294647794

```

So it’s not about if `size(x,2)==1`, it’s about the type of the object. Usually code where `x` is sometimes a vector and sometimes a matrix is avoided (it’s usually [type unstable](https://docs.julialang.org/en/v1/manual/performance-tips/#Write-%22type-stable%22-functions)), but if `y` is a vector, you can `reshape` it to be an `n x 1` matrix via

```julia
julia> y = rand(4)
4-element Vector{Float64}:
 0.2646642144471312
 0.5108444025487056
 0.17038220190741749
 0.6299368977104578

julia> reshape(y, :, 1)
4×1 Matrix{Float64}:
 0.2646642144471312
 0.5108444025487056
 0.17038220190741749
 0.6299368977104578

```

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [March 30, 2021, 10:10am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/3 "2021-03-30T10:10:40Z")

</div>

Or more compact: `hcat(y)`, although that makes a copy of `y`. (`reshape` keeps the same underlying data).

---

<div class="post-metadata">

### Author: ![ovt](https://avatars.discourse-cdn.com/v4/letter/o/87869e/32.png) [@ovt](https://discourse.julialang.org/u/ovt)
#### Post date: [March 30, 2021, 10:13am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/4 "2021-03-30T10:13:53Z")

</div>

Thanks for your answer!

Then, how do I extract a column of a matrix and avoid the automatic vector conversion? Is reshape the only built in solution?

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [March 30, 2021, 10:15am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/5 "2021-03-30T10:15:35Z")

</div>

> [@ovt](#):
>
> how do I extract a column of a matrix and avoid the automatic vector conversion?

Indexing with ranges that only contain the value of the column you want:

```julia
julia> x = rand(4,2)
4×2 Array{Float64,2}:
 0.713682 0.122839
 0.488861 0.225701
 0.742566 0.451154
 0.361401 0.851278

julia> x[:,1:1]
4×1 Array{Float64,2}:
 0.7136823322753825
 0.48886127651427835
 0.7425656894856532
 0.3614009764175452

```

---

<div class="post-metadata">

### Author: ![ovt](https://avatars.discourse-cdn.com/v4/letter/o/87869e/32.png) [@ovt](https://discourse.julialang.org/u/ovt)
#### Post date: [March 30, 2021, 10:20am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/6 "2021-03-30T10:20:14Z")

</div>

Thanks!

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [March 30, 2021, 10:25am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/7 "2021-03-30T10:25:06Z")

</div>

And a similar option: indexing with a vector with one element:

```julia
julia> x = rand(4,2)
4×2 Array{Float64,2}:
 0.713682 0.122839
 0.488861 0.225701
 0.742566 0.451154
 0.361401 0.851278

julia> x[:,[1]]
4×1 Array{Float64,2}:
 0.7136823322753825
 0.48886127651427835
 0.7425656894856532
 0.3614009764175452

```

(The key is that if the type of the index can contain more than one value, it will return a matrix instead of a vector.)

---

<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: [March 30, 2021, 10:31am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/8 "2021-03-30T10:31:29Z")

</div>

Can’t you just work with vectors instead of one-column matrices?

---

<div class="post-metadata">

### Author: ![ovt](https://avatars.discourse-cdn.com/v4/letter/o/87869e/32.png) [@ovt](https://discourse.julialang.org/u/ovt)
#### Post date: [March 30, 2021, 10:38am UTC](https://discourse.julialang.org/t/sort-and-dims-1-for-vector-input/58212/9 "2021-03-30T10:38:08Z")

</div>

No because I work with matrices, and column matrices consist of a particular case that I have to take into account.
