Find which columns have duplicates

Hey there,
Im writing a code where I have to find which columns of an array have duplicates in them.
I already have found a way to find rows with duplicates but I'm not sure how to make this for columns.
Considering the following: A is a 2d matrix
A = [7 3 1 4 9 8 5 6
9 6 5 8 2 3 7 4
8 5 7 6 3 1 9 2
6 1 9 7 8 4 2 3
3 2 6 5 4 9 1 7
4 9 2 3 1 4 8 5
5 8 3 2 6 7 4 1
1 7 4 9 5 2 3 8];
RowsWithDuplicates = find(arrayfun(@(i)(~isequal (length (unique (A(i,:))), size(A,2))), 1:size(A,1)))
RowsWithDuplicates = 6
I'm not sure how this exactly works but it does.
How can I make it so it finds which columns have duplicates?

回答 (1 件)

Torsten
Torsten 2022 年 12 月 8 日

0 投票

Transpose the matrix :-)
Or:
ColumnsWithDuplicates = find(arrayfun(@(i)(~isequal (length (unique (A(:,i))), size(A,1))), 1:size(A,2)))

2 件のコメント

Jochem
Jochem 2022 年 12 月 8 日
Thanks this works!
Rik
Rik 2022 年 12 月 8 日
Just to provide the commentary:
The arrayfun will run the function on the input data, in this case the vector 1:size(A,1).
The function takes a scalar input i, and compares length(unique(A(i,:))) to size(A,2). So it counts the number of elements after unique has done its thing. If those two are the same, that means there are no duplicates. The isequal function will test this.
The last step is that the find function will convert this logical vector to row indices.
The code that Torsten posted just swapped all rows and columns.

サインインしてコメントする。

カテゴリ

製品

質問済み:

2022 年 12 月 8 日

コメント済み:

Rik
2022 年 12 月 8 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by