Finding unique columns of a matrix

Dear all,
I have a matrix A:

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.

You could do

reduce(hcat, unique(eachcol(A))
4 Likes

Works perfectly! Very easy.

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

PS: your example is a bit unfortunate as no columns are repeated :slight_smile:

7 Likes