Search code examples
matlabunique

How to find a unique (non-repeated) value in a matrix by using matlab


everyone. Let's say I have following (3x3)matrix A:

0 1 3
0 0 3
0 0 0

My question is how to find out the unique value in that matrix by using matlab? In this case, the result should be 1. I have tried used the

value=unique(A)

but it returned a vector {0;1;3} is not what I want.

I much appreciate if you guys can help me solve this problem. Thank you!


Solution

  • Here is a short one

    value = A(sum(bsxfun(@eq, A(:), A(:).'))==1);
    

    It compares all pairs of elements in the matrix and counts how many times they are equal and returns the ones that have been counted only once.