Check Duplicate Entries in Matrix then remove all but one entry

Discussion in 'MATLAB' started by Ed Chamberlayne, Sep 29, 2009.

  1. I have a matrix Z:
    [ 1 24 96
    2 10 20
    3 12 27
    4 14 40
    5 16 50
    6 18 60
    7 20 72
    8 22 85
    1 34 116
    2 20 40
    1 36 123
    2 22 47
    3 24 54]

    I would like to check for duplicate entries in the 2nd column. I want to keep the row that has the highest 3rd column entry and then delete the other rows.

    For instance, the value of 20 is repeated in the 2nd column twice. I want to compare row [7 20 72] and row [2 20 40]. Since 72 is greater than 40, I want to then delete row [2 20 40]. Does this make sense?

    I'm having trouble separating out the duplicate entries. Then it should be a max function. Then setting the row that needs to be deleted to [];

    Thanks,

    Ed
     
    Ed Chamberlayne, Sep 29, 2009
    #1
    1. Advertisements

  2. Ed Chamberlayne

    Jos Guest

    First sort the matrix according to the 2nd and 3rd column using sortrows, then apply UNIQUE on the second column.

    % data: keep row 2 and row 4 (here indicated by 1st column)
    M = [1 1 2 ; 2 2 6 ; 3 2 1 ; 4 1 5 ; 5 2 3]

    M2 = sortrows(M,[2 3])
    [dummy, idx] = unique(M2:),2))
    R = M2(idx,:)

    hth
    Jos
     
    Jos, Sep 30, 2009
    #2
    1. Advertisements

  3. Ed Chamberlayne

    Antonietta Guest

    I have got a similar problem:
    I have got a Nx2 matrix containing the X and Y coordinates of N pints in 2D. I would like to check for duplicate rows in the matrix, keep only one of those rows and replace all the others by the nearest pair (x,y) which is not in the matrix. In other words I would like to replace the x- and y- coordinates of duplicate points (which are rows in the matrix) with the x- and y- coordinates of their closest point (in term of euclidean distance). These points (x,y coordinates) have to be new.
    Any idea?
     
    Antonietta, May 14, 2011
    #3
  4. Ed Chamberlayne

    Antonietta Guest

    I have got a similar problem:
    I have got a Nx2 matrix containing the X and Y coordinates of N pints in 2D. I would like to check for duplicate rows in the matrix, keep only one of those rows and replace all the others by the nearest pair (x,y) which is not in the matrix. In other words I would like to replace the x- and y- coordinates of duplicate points (which are rows in the matrix) with the x- and y- coordinates of their closest point (in term of euclidean distance). These points (x,y coordinates) have to be new.
    Any idea?
     
    Antonietta, May 14, 2011
    #4
  5. Ed Chamberlayne

    Greg Heath Guest

    I don't understand where the new points are coming from.

    Greg
     
    Greg Heath, May 14, 2011
    #5
  6. Ed Chamberlayne

    Antonietta Guest

    The new points are pairs of intiger numbers not in the matrix.
    As an example, for a matrix M
    M=[1, 2; 2, 3; 3,2; 3, 1; 1, 1; 2, 2; 2, 2; 3, 3]
    the point (2,2) has to be replaced with a new pair pair (x,y) such that:
    - the pair (x,y) is not any of the rows of M
    -among all the possible pairs of two intiger numbers not in M, I should pick
    the pair (x,y) having minimum distance from the point (2,2)
    In the considered example one of the two pairs (2,2) (I do not care, it could be the first or the second one) should be replaced by the point (2,1). Eventually, also the point (1,3) could be accetable.

    I hope the problem is now clear
     
    Antonietta, May 14, 2011
    #6
  7. Ed Chamberlayne

    Greg Heath Guest

    I would use an auxiliary 3 X 3 binary matrix
    indicating which points are and are not used.

    I'll leave the logic for you. As long as
    the points are on a regular grid, it shouldn't
    be difficult.

    Hope this helps.

    Greg
     
    Greg Heath, May 14, 2011
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.