How to delete an repeated values in matrix?

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

Jan
Jan 2015년 2월 9일
Please post the wanted result also. Should the columns be unique? Or should all columns vanish, which appear more than once? Or do you mean the single elements?
wanted result:
p=[11.6502 41.6502
51.2008 61.2008];

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2015년 2월 9일
편집: Stephen23 2015년 2월 9일

0 개 추천

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

Matlab111
Matlab111 2015년 2월 9일
편집: Matlab111 2015년 2월 9일
Stephen Cobeldick- i'm getting an error like this "Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other."
Stephen23
Stephen23 2015년 2월 9일
편집: Stephen23 2015년 2월 9일
I suspect that your array A is not a vector. I have slightly altered my code to work regardless of the shape of the input array A:
>> 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 51.2008 41.6502 61.2008
Also I repeat the advice that I gave earlier: do not test for equality == or use unique to solve this problem.
Matlab111
Matlab111 2015년 2월 9일
편집: Matlab111 2015년 2월 9일
Stephen Cobeldick- ya i'm not getting sir, try this one
question:
c=[0.7893 0.8337 0.1479 0 0 0.1479 0.9993];
1.now i should delete the repeated values in that 'c'.
2.i should delete the values that is displayed before the zeros.
3.And finally i should delete zeros also.
Expected output:
d=[0.9993];
Stephen23
Stephen23 2015년 2월 9일
편집: Stephen23 2015년 2월 9일
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:

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Roger Stafford
Roger Stafford 2015년 2월 9일

0 개 추천

If x is your array with repetitions
[~,ia] = unique(x,'first','legacy');
x = x(sort(ia));
Andrei Bobrov
Andrei Bobrov 2015년 2월 9일
편집: Andrei Bobrov 2015년 2월 9일

0 개 추천

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);

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2015년 2월 9일

편집:

2015년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by