How to delete an repeated values in matrix?

8 views (last 30 days)
Arul prasath
Arul prasath on 9 Feb 2015
Edited: Stephen on 9 Feb 2015
I have matrix like this, so how to delete repeated values in this case?
[a b]=[197.9040 11.6502 41.6502 41.3856 41.3856 0 197.9040
12.2180 51.2008 61.2008 104.3122 104.3122 0 12.2180];
  2 Comments
Arul prasath
Arul prasath on 9 Feb 2015
wanted result:
p=[11.6502 41.6502
51.2008 61.2008];

Sign in to comment.

Accepted Answer

Stephen
Stephen on 9 Feb 2015
Edited: Stephen on 9 Feb 2015
A = [197.9040, 11.6502, 41.6502, 41.3856, 41.3856, 0, 197.9040, 12.2180, 51.2008, 61.2008, 104.3122, 104.3122, 0, 12.2180];
>> A(sum(bsxfun(@(a,b)abs(a-b)<1e-6,A,A(:)))<2)
ans =
11.6502 41.6502 51.2008 61.2008
Because your values are floating point it is best to avoid using eq, unique and the like, which only work for exactly identical values . Instead I used a tolerance of 1e-6, and values closer than this tolerance are assumed to be the same. You can change the tolerance to suit your values.
  4 Comments
Stephen
Stephen on 9 Feb 2015
You original question, which my code above solves exactly, does not mention anything about removing values before any zeros. You are changing the requirements, which makes it difficult for us to help you.
It is considered polite to Accept answers that solve your original question. I have answered your other question, about removing leading values, here:

Sign in to comment.

More Answers (2)

Roger Stafford
Roger Stafford on 9 Feb 2015
If x is your array with repetitions
[~,ia] = unique(x,'first','legacy');
x = x(sort(ia));

Andrei Bobrov
Andrei Bobrov on 9 Feb 2015
Edited: Andrei Bobrov on 9 Feb 2015
x = [197.9040 11.6502 41.6502 41.3856 41.3856 0 197.9040
12.2180 51.2008 61.2008 104.3122 104.3122 0 12.2180];
x = x(:);
[xx,~,c] = unique(x);
b = histc(c,1:max(c));
out = xx(b==1);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!