Identifying duplicates and deleting duplicates

조회 수: 4 (최근 30일)
Matt
Matt 2011년 8월 18일
v1 =[300 350 350 350 360 370 380 380 380 390 395 405 450 450 450 450 450 450 500 500 500 500 500 500]
v2 = [NaN 1 2 3 3 NaN 1 2 3 NaN 2 NaN 1 2 3 1 2 3 1 2 3 1 2 1 ]
I am trying to make a script which identifies data which is repeated in v1 and v2 (when they are combined) and then deletes the repeated data in both v1 and v2.
I've been looking at this problem for a long time, I really don't know where to start. I started of using a = find(diff(v1)==0) but that didn't get me anywhere. I'm very bad at explaining my problems, here is the desired output of the script(may help with understanding the problem):
v1 =[300 350 350 350 360 370 380 380 380 390 395 405 450 450 450 500 500 500]
v2 = [NaN 1 2 3 3 NaN 1 2 3 NaN 2 NaN 1 2 3 1 2 3 ]
  댓글 수: 3
Sean de Wolski
Sean de Wolski 2011년 8월 18일
The output isn't clear to me. So you want to find values that are repeated in both v1 &v2?
So, why are there fewer 500s in the output of v1? There's not repeated... Is it a sequence you want rid of? [1 2 3] is repeated three times in v2 at the end but is only one + the end is removed. Sorry, not clear at all....
Matt
Matt 2011년 8월 18일
I'm sorry, I'll try to make it clearer.
It's not a sequence I want rid of it is duplicates the reason the 500's were deleted was because there was 500-1, 500-2, 500-3, 500-1, 500-2 and 500-1 therefore two of the 500-1's are repeats and one of the 500-1's is a repeat, therefore they are removed.

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 18일
Use unique( ,'rows')
v1 =[300 350 350 350 360 370 380 380 380 390 395 405 450 450 450 450 450 450 500 500 500 500 500 500]
v2 = [NaN 1 2 3 3 NaN 1 2 3 NaN 2 NaN 1 2 3 1 2 3 1 2 3 1 2 1 ]
v1_out =[300 350 350 350 360 370 380 380 380 390 395 405 450 450 450 500 500 500]
v2_out = [NaN 1 2 3 3 NaN 1 2 3 NaN 2 NaN 1 2 3 1 2 3 ]
out=unique([v1' v2'],'rows');
z1=transpose(out(:,1));
z2=transpose(out(:,2));
isequal(v1_out,z1)
isequalwithequalnans(v2_out,z2)
If v2 is cell array of strings, then NaN is not an issue.
v1=[1 1 1 11 12];
v2={'a','ab','a','b','a'};
str=[num2str(v1'),char(v2)];
[str,index]=unique(cellstr(str));
v1=v1(index)
v2=v2(index)
  댓글 수: 5
Fangjun Jiang
Fangjun Jiang 2011년 8월 18일
@Matt, you should use Sean's solution, which considered the case for comparing NaNs.
Matt
Matt 2011년 8월 18일
Thanks

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

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 8월 18일
vcat = [v1', v2'];
vcat(isnan(vcat)) = inf; %assuming no infs!
vu = unique(vcat,'rows')
vu(isinf(vu))= nan;
v1 = vu(:,1).';
v2 = vu(:,2).';
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 8월 18일
Good to consider the case for nan
Matt
Matt 2011년 8월 18일
This works, only problem is I forgot to mention that v2 is a string variable! I put it like that to make the explanation easier, I can't get your solution to work when one of them is a string variable!
Any ideas, silly of me not to mention it at the start I know

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


Friedrich
Friedrich 2011년 8월 18일
Hi,
are you looking for the unique command?
>> a = [1 2 2 3 1 4]
a =
1 2 2 3 1 4
>> unique(a)
ans =
1 2 3 4
  댓글 수: 1
Matt
Matt 2011년 8월 18일
i'm looking into concatenation, i think it may help

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by