Better formulation of looking for duplicate numbers

Discussion in 'MATLAB' started by Jakub Nedbal, Nov 2, 2004.

  1. Jakub Nedbal

    Jakub Nedbal Guest

    I am sorry for my poor English - A matrix of different positive
    integers should be a partial result of my program. The program always
    produces in each cycle a unique matrix as a result of some
    complicated calculations. But sometimes things goes wrong and some
    numbers appear twice in the matrix. That should not happen. I want to
    find these duplicate numbers and refine the calculation for these
    numbers - so my point is not to generate an arbitrary matrix of
    different positive integers but to find mistakes in my resulting
    model. thanks alot jakub
     
    Jakub Nedbal, Nov 2, 2004
    #1
    1. Advertisements

  2. Jakub Nedbal

    Ashu Guest

    Hi Jakub.. how about something like..

    mat_size=size(yourmatrix);
    for i=1:mat_size(1,1)
    for j=1:mat_size(1,2)
    locs=find(yourmatrix==matrix(i,j));

    if(length(locs)>1)
    % dO THE repeat calculation for locs
    end
    end
    end

    i am sure there is a more elegant way of doing this...but this might
    help.

    Ashu
     
    Ashu, Nov 2, 2004
    #2
    1. Advertisements

  3. Jakub Nedbal

    per isakson Guest

    Results=magic(3);
    Results=cat( 1, Results, Results( 1,:) )
    [b,m,n]=unique(Results);
    Results(m)=[] % prints the copies

    / per
     
    per isakson, Nov 2, 2004
    #3
  4. Jakub Nedbal

    jakub nedbal Guest

    Hi! Thanks. Well this is a way how to get around but it's not good.
    It's better to avoid the sequential algorithms like this "for-end". I
    am working with huge chunks of data and scanning through the whole
    matrix would take too long. I was wondering if there was some exact
    command to deal with this or some ingenious trick someone has
    invented. I will try to think hard to come up with one and post it
    here.
    Anyway thanks for your time... jakub
     
    jakub nedbal, Nov 2, 2004
    #4
  5. Try using hist.

    Consider:

    A = floor(rand(10)*100);
    [n,x] = hist(A:)),unique(A:))));

    The n's will tell you the number of times each x occurs in A. I leave it to
    you to find the repeated values in your A matrix.

    Cheers,
    Brett
     
    Brett Shoelson, Nov 2, 2004
    #5
  6. Jakub Nedbal

    Ashu Guest

    The reason - why posted this was:
    1. My ignorance to "unique" :)
    2. I feel that now for loops do run as fast as vectorized code and
    that said - if you do have a large matrix - sooner or later i imagine
    one has to bite the bullet in terms of comp. time.

    The way - look at it is - no matter how it's done - physically the
    process has to sieve through all elements once to look for any
    duplicacies..

    That's just my rather limited take..

    Regards,

    Ashu
     
    Ashu, Nov 2, 2004
    #6
  7. Jakub Nedbal

    jakub nedbal Guest

    Per: Brett:

    Thanks a lot - You have the right ways of doing this. I am happy.

    regards.
     
    jakub nedbal, Nov 2, 2004
    #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.