Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

How to remove duplicate rows from matrix

Date: 2022-07-26 11:18:49

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. 

Answers: 

See http://www.mathworks.com/help/techdoc/ref/unique.html

b = unique(A, 'rows') returns the unique rows of A. 

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 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 set) {
        Integer[][] arr = new Integer[set.size()][];
        Iterator 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);
        }
    }
}

 


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com