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. 

Expert Answer:

s: 

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 ?

Looking for reliable MATLAB assignment help? Our expert MATLAB tutors deliver high-quality, easy-to-understand solutions tailored to your academic needs. Whether you're studying at Monash University, the University of Sydney, UNSW, or the University of Melbourne, we provide trusted MATLAB assistance to help you excel. Contact us today for the best MATLAB solutions online and achieve academic success!

MATLAB Assignment Help Services

Personalized Tutoring: Get one-on-one guidance from our MATLAB experts. Whether you're tackling basic concepts or advanced algorithms, we provide clear, step-by-step explanations to help you master MATLAB with confidence.

Assignment Assistance: Struggling with tight deadlines or complex assignments? Our team offers end-to-end support, from problem analysis to code development and debugging, ensuring your assignments meet the highest academic standards.

Project Development: Need expert help with your MATLAB research project? We assist in designing and implementing robust solutions, covering project planning, data collection, coding, simulation, and result analysis.

Coursework Support: Enhance your understanding of MATLAB with our comprehensive coursework assistance. We help you grasp lecture concepts, complete lab exercises, and prepare effectively for exams.

Thesis and Dissertation Guidance: Incorporate MATLAB seamlessly into your thesis or dissertation. Our experts provide support for data analysis, modeling, and simulation, ensuring your research is methodologically sound and impactful.

Contact us on WhatsApp for MATLAB help

Contact us on Telegram for MATLAB solutions