How to use unique in an mxn matrix without sorting the columns?

3 次查看(过去 30 天)
For my project I have to account for any duplicate columns and remove them, but the original order of the columns must be retained. If I use the method below, the columns in the output get sorted. Is there a way to remove the duplicate columns in a matrix without sorting them?
I have a 2 by 4 matrix A:
A = [7, 2, 1, 9; 3, 2, 9, 11]
a_transpose = A.'
a_no_duplicate = unique(a_transpose, 'rows').'
After running the code, this is the output:
a_no_duplicate =
1 2 7 9
9 2 3 11
(Here the columns are sorted)

采纳的回答

Matt J
Matt J 2021-9-9
编辑:Matt J 2021-9-9
A = [7, 2, 1, 1; 3, 2, 9, 9]
A = 2×4
7 2 1 1 3 2 9 9
a_no_duplicate = unique(A.', 'rows').'
a_no_duplicate = 2×3
1 2 7 9 2 3
a_no_duplicate_no_sort = unique(A.', 'rows','stable').'
a_no_duplicate = 2×3
7 2 1 3 2 9

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by