# Choosing only different vectors from a matrix

**URL:** <https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237>\
**Category:** New to Julia\
**Created:** [August 31, 2019, 2:17pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237 "2019-08-31T14:17:12Z")\
**Posts on this page:** 9\
**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:** [August 31, 2019, 2:17pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/1 "2019-08-31T14:17:13Z")

</div>

Hello users,  
I have a matrix A:

```julia
 A=[1.0 7.0 4.0 8.0 9.0 4.0 5.0 0.0 10.0 10.0 1.0 7.0
     1.0 0.0 0.0 9.0 8.0 1.0 3.0 5.0 7.0 1.0 1.0 0.0
     1.0 0.0 0.0 5.0 10.0 8.0 3.0 5.0 1.0 5.0 1.0 0.0];

```

How can I choose only the columns with different elements?  
Is there a command for this?  
I this example, the columns from 1 to 10 are different and columns 11 and 12 are repeated.  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 31, 2019, 2:42pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/2 "2019-08-31T14:42:28Z")

</div>

`unique(eachcol(A))` will filter for the unique columns, however, if you just want to keep columns which are not repeated, you can count them and then filter, eg:

```julia
using DataStructures
C = counter(eachcol(A))
filter(x -> last(x) == 1, C.map)

```

Note that if you are doing anything numerical, floating point error may make the simple notion of equality implicit in these examples quite useless.

---

<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:** [August 31, 2019, 7:04pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/3 "2019-08-31T19:04:19Z")

</div>

> [@Tamas\_Papp](#):
>
> using DataStructures C = counter(eachcol(A)) filter(x → last(x) == 1, C.map)

I have used this commands but an error appears:

```julia
UndefVarError: eachcol not defined
top-level scope at none:0

```

I am using Julia 1.0.4 and I have installed the package DataStructures.

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [August 31, 2019, 7:17pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/4 "2019-08-31T19:17:19Z")

</div>

what version of Julia are you using? `eachcol` was added at in v1.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:** [August 31, 2019, 7:18pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/5 "2019-08-31T19:18:21Z")

</div>

I am with Julia 1.0.4.  
Is there a solution for this version?  
Thanks so much!

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [August 31, 2019, 7:21pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/6 "2019-08-31T19:21:16Z")

</div>

you can add `OnlineStats` and use `OnlineStats.eachcol`. but never tested it

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 31, 2019, 10:26pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/7 "2019-08-31T22:26:56Z")

</div>

Perhaps adding all those packages may be overkill. How about the simple, albeit verbose solution:

```julia
function in_matrix(v, m)
	inMatrix = false;
	(_, nCols) = size(m);
	for ic = 1 : nCols
		if v == @view m[:,ic]
			inMatrix = true;
			break;
		end
	end	
	return inMatrix
end

function drop_duplicates(B)
	@views begin
		(_, nCols) = size(B);
		kept = trues(nCols);
		for ic = 1 : (nCols - 1)
			if in_matrix(B[:,ic], B[:, (ic+1) : nCols])
				kept[ic] = false;
			end
		end
	end
	return B[:, kept]
end

A=[1.0 7.0 4.0 8.0 9.0 4.0 5.0 0.0 10.0 10.0 1.0 7.0
	 1.0 0.0 0.0 9.0 8.0 1.0 3.0 5.0 7.0 1.0 1.0 0.0
	 1.0 0.0 0.0 5.0 10.0 8.0 3.0 5.0 1.0 5.0 1.0 0.0];
 
# Avoid numerical issues with isequal for this example
B = round.(Int, A);

drop_duplicates(B)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 1, 2019, 5:43am UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/8 "2019-09-01T05:43:09Z")

</div>

You can always operate on eg

```julia
[A[:, i] for i in axes(A, 2)]

```

instead, but frankly, I would just upgrade to 1.2 unless you have a compelling reason not to.

---

<div class="post-metadata">

**Author:** ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)\
**Post date:** [September 1, 2019, 7:51pm UTC](https://discourse.julialang.org/t/choosing-only-different-vectors-from-a-matrix/28237/9 "2019-09-01T19:51:15Z")

</div>

You could just copy/paste the implementation of `eachcol`:

```julia
eachcol(A) = (view(A, :, i) for i in axes(A, 2))

```
