MATLAB: Find and remove a value from an array


Hello! I'm trying to find in an array 'done' the position of a value 'next' without the use of a loop. Then, in this array i want to remove from that position found, the existing element.
I tried it this way but is doesnt seem to work. Would you find an arror or recommend another way?

Best Answer

  • Hi Zenia Askar,
    Assuming the content of your variables 'done' and 'next' are numeric your code cannot remove a single value from the matrix without destroying the matrix structure. You should apply logical indexing rather than using 'find()'.
    done(done == next) = [];
    The result does not represent the matrix structure anymore but is a vector then. To omit that you can place a NaN-value instead.
    done(done == next) = NaN;
    Kind regards,