# Finding unique columns of a matrix

**URL:** <https://discourse.julialang.org/t/finding-unique-columns-of-a-matrix/57421>\
**Category:** New to Julia\
**Created:** [March 17, 2021, 10:07pm UTC](https://discourse.julialang.org/t/finding-unique-columns-of-a-matrix/57421 "2021-03-17T22:07:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 17, 2021, 10:07pm UTC](https://discourse.julialang.org/t/finding-unique-columns-of-a-matrix/57421/1 "2021-03-17T22:07:57Z")

</div>

Dear all,  
I have a matrix A:

```julia
A = [  
 1.0 0.0 0.0 0.0 1.0 1.0 3.0 0.0 0.0 1.0
  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0
  1.0 0.0 0.0 0.0 1.0 11.0 0.0 0.0 0.0 1.0
  0.0 6.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0
 12.0 0.0 0.0 19.0 0.0 0.0 0.0 0.0 1.0 0.0
  0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
  1.0 0.0 0.0 1.0 0.0 0.0 3.0 11.0 3.0 0.0
  0.0 0.0 8.0 0.0 1.0 2.0 0.0 1.0 1.0 3.0
  1.0 1.0 0.0 2.0 0.0 0.0 3.0 0.0 0.0 0.0
 15.0 0.0 0.0 1.0 2.0 0.0 1.0 0.0 1.0 0.0
  0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 6.0 7.0
  0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 5.0 0.0 0.0 0.0 2.0 0.0
  0.0 0.0 2.0 0.0 1.0 1.0 3.0 0.0 0.0 0.0
  0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0]

```

(may has much more columns). There is a command to eliminate the repeated columns and determine a smaller matrix, with the same number of rows, whose columns are all different?  
Many thanks.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 17, 2021, 10:09pm UTC](https://discourse.julialang.org/t/finding-unique-columns-of-a-matrix/57421/2 "2021-03-17T22:09:57Z")

</div>

You could do

```julia
reduce(hcat, unique(eachcol(A))

```

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 17, 2021, 10:11pm UTC](https://discourse.julialang.org/t/finding-unique-columns-of-a-matrix/57421/3 "2021-03-17T22:11:14Z")

</div>

> [@pdeffebach](#):
>
> `reduce(hcat, unique(eachcol(A))`

Works perfectly! Very easy.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 18, 2021, 12:07am UTC](https://discourse.julialang.org/t/finding-unique-columns-of-a-matrix/57421/4 "2021-03-18T00:07:42Z")

</div>

I think this is equivalent to: ` unique(A,dims=2)`

_PS: your example is a bit unfortunate as no columns are repeated 🙂_
