Change or remove duplicate matrix elements

I have the matrix:
value =
3.1727
5.2495
5.2708
3.3852
5.6222
5.2708
5.1444
4.9834
5.7499
5.7499
3.4728
3.4728
5.3560
5.7499
3.4728
5.7286
6.1225
3.6539
3.0351
4.3020
5.2296
3.8040
4.6747
5.4412
3.6539
and I want to add 0.1 to any duplicate entries, so that all values are unique, and none are removed.
I have tried:
value=sort(value)
for i=1:(length(value)-1)
if value(i+1)==value(i);
value(i+1)=(value(i+1)+0.1);
end
end
But for some reason it has no impact on the matrix..
Your help is greatly appreciated, thanks!
**EDIT: how can i remove all but the first occurance of a duplicate value? unique does not work for me.

댓글 수: 2

Sean de Wolski
Sean de Wolski 2011년 8월 26일
So what if by adding 0.1 you duplicate a value? You'll have to do a while loop checking to ensure this didn't happen and rerunning the engine again if it did.
A
A 2011년 8월 26일
That's true. What if on the other hand I want to completely remove any duplicate entries?

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 8월 26일

0 개 추천

X = [1 2 3 3 4 5 2]';
X2 = sort(X,1);
idx = [false;diff(X2)<(10^-6)]; %equal to 10^-6th precision
X2(idx) = X2(idx)+.1;

댓글 수: 8

A
A 2011년 8월 26일
Thanks for your response.
This is the output with my matrix:
3.0351
3.1727
3.3852
3.4728*
3.4728*
3.4728*
3.6539
3.7539
3.8040
4.3020
4.6747
4.9834
5.1444
5.2296
5.2495
5.2708
5.3708
5.3560
5.4412
5.6222
5.7286
5.7499*
5.7499*
5.8499
6.1225
As in the response above, it works on some entires but entirely skips others..
Jan
Jan 2011년 8월 26일
Yup. Then these values are *not* equal.
The two 5.7499 can be 5.749900000000001 and 5.74989999999999.
Sean de Wolski
Sean de Wolski 2011년 8월 26일
you could easily add a tolerance to the diff operator, I'll do it in a second.
Sean de Wolski
Sean de Wolski 2011년 8월 26일
Try it now, they need to be accurate to 10^-6 places. Tighten or loosen the tolerance as you wish
A
A 2011년 8월 26일
Great, I used it twice and all of the values are now unique.
I am also interested to know how I can remove all but the first instance of a repeated value. unique does not work, and I suspect this may have something to do with the tolerence, as you mentioned. I have yet to decide of removing or changing the multiple values is more ideal for my task. Thank you for your help!
A
A 2011년 8월 26일
ah, just did X2(idx)=[]
Thanks so much for your help!
Fangjun Jiang
Fangjun Jiang 2011년 8월 26일
@Sean, this won't work. The problem is not the floating point comparison. It's the multiple duplication. If the first round has 3 duplications, then after the round, you still have 2 duplications.
X = [1 2 3 3 4 5 2]';
X2 = sort(X,1);
idx = [false;diff(X2)<(10^-6)]; %equal to 10^-6th precision
X2(idx) = X2(idx)+.1
Sean de Wolski
Sean de Wolski 2011년 8월 26일
Thst's true, hence the while-loop I mentioned in the comment above.

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 8월 26일

0 개 추천

It works in certain degree but your algorith has a flaw. You'll see it clearly running the following.
value=sort(value);
NewValue=value;
for i=1:(length(NewValue)-1)
if NewValue(i+1)==NewValue(i);
NewValue(i+1)=(NewValue(i+1)+0.1);
end
end
[value NewValue]

댓글 수: 3

A
A 2011년 8월 26일
Thank you for your response!
This is what is returned:
3.0351 3.0351
3.1727 3.1727
3.3852 3.3852
3.4728 3.4728 ***
3.4728 3.4728 ***
3.4728 3.4728 ***
3.6539 3.6539
3.6539 3.7539
3.8040 3.8040
4.3020 4.3020
4.6747 4.6747
4.9834 4.9834
5.1444 5.1444
5.2296 5.2296
5.2495 5.2495
5.2708 5.2708
5.2708 5.3708
5.3560 5.3560
5.4412 5.4412
5.6222 5.6222
5.7286 5.7286
5.7499 5.7499
5.7499 5.7499 ***
5.7499 5.8499
6.1225 6.1225
it worked in some places, but the (***) mark where it didn't work at all.. this is very confusing. Any idea why this is happening?
A
A 2011년 8월 26일
Ah sorry thats a bit hard to read, I didn't realize the numbers woudl be so close together. It is [value NewValue] as you put in your code.
Fangjun Jiang
Fangjun Jiang 2011년 8월 26일
The reason is simple. You added 0.1 to the 2nd duplicated value which will change the comparison of your next loop.

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

MaVo
MaVo 2016년 2월 2일

0 개 추천

I had a similar issue with a time sequence in 0.005 s steps, but I figured out a solution that worked for me. My problem during debugging was, that it didn't go into the if-condition. I solved this by comparing integers than double values. Maybe this helps someone.
s_length = length(s_time);
ctr = 1;
for ctr = 2:s_length
% Checking values for debugging
new = uint64(s_time(ctr,1)/0.005);
old = uint64(s_time((ctr-1),1)/0.005);
before = (ctr-1);
if new == old
s_time(ctr:end,1) = s_time(ctr:end,1)+0.005;
end
if new > (old + 1)
s_time(ctr:end) = s_time(ctr:end) - 0.005;
end
end

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

질문:

A
A
2011년 8월 26일

편집:

2016년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by