I want to remove duplicate rows from a matrix. I read How can I remove duplicates in an array but keep the same order?, but this is not exactly what I want.
我想从矩阵中删除重复的行。我读到如何删除数组中的重复内容,但保持相同的顺序?但这并不是我想要的。
The solution above removes duplicate values (cells) from matrix (and returns a vector), but I need to remove duplicate rows and return a matrix — the same matrix without duplicate rows.
上面的解决方案从矩阵中移除重复的值(单元格)(返回一个向量),但是我需要删除重复的行并返回一个矩阵——相同的矩阵没有重复的行。
Example:
例子:
a = [1,2; 3,4; 5,6; 1,2; 7,8]
a =
1 2
3 4
5 6
1 2
7 8
%...
ans =
1 2
3 4
5 6
7 8
The order doesn't matter.
的顺序并不重要。
2 个解决方案
#1
14
See http://www.mathworks.com/help/techdoc/ref/unique.html
参见http://www.mathworks.com/help/techdoc/ref/unique.html
b = unique(A, 'rows') returns the unique rows of A.
b = unique(A, 'rows')返回A的唯一行。
#2
0
Here is my solution
这是我的解决方案
package com.test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class DuplicateInMatrix {
public static void main(String[] args) {
Integer[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 1, 2, 3 } };
Set<Element> set = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
set.add(new Element(arr.length, arr[i]));
}
buildResultArray(set);
}
private static void buildResultArray(Set<Element> set) {
Integer[][] arr = new Integer[set.size()][];
Iterator<Element> itr = set.iterator();
for (int i = 0; i < arr.length && itr.hasNext(); i++) {
arr[i] = itr.next().row;
}
printArrray(arr);
}
private static void printArrray(Integer[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
static class Element {
int n;
Integer[] row = new Integer[n];
public Element(int n, Integer[] row) {
this.n = n;
this.row = row;
}
@Override
public int hashCode() {
return Arrays.hashCode(row);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Element other = (Element) obj;
return Arrays.deepEquals(this.row, other.row);
}
@Override
public String toString() {
return Arrays.toString(row);
}
}
}
#1
14
See http://www.mathworks.com/help/techdoc/ref/unique.html
参见http://www.mathworks.com/help/techdoc/ref/unique.html
b = unique(A, 'rows') returns the unique rows of A.
b = unique(A, 'rows')返回A的唯一行。
#2
0
Here is my solution
这是我的解决方案
package com.test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class DuplicateInMatrix {
public static void main(String[] args) {
Integer[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 1, 2, 3 } };
Set<Element> set = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
set.add(new Element(arr.length, arr[i]));
}
buildResultArray(set);
}
private static void buildResultArray(Set<Element> set) {
Integer[][] arr = new Integer[set.size()][];
Iterator<Element> itr = set.iterator();
for (int i = 0; i < arr.length && itr.hasNext(); i++) {
arr[i] = itr.next().row;
}
printArrray(arr);
}
private static void printArrray(Integer[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
static class Element {
int n;
Integer[] row = new Integer[n];
public Element(int n, Integer[] row) {
this.n = n;
this.row = row;
}
@Override
public int hashCode() {
return Arrays.hashCode(row);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Element other = (Element) obj;
return Arrays.deepEquals(this.row, other.row);
}
@Override
public String toString() {
return Arrays.toString(row);
}
}
}