Reputation: 259
I have two column vectors X and Y of the same size derived by following Matlab code:
mask = im2bw(rgb2gray(imread('https://i.sstatic.net/ES8w1.jpg')));
separation = bwulterode(mask,'euclidean',[0 0 0; 1 1 1; 0 0 0]).*mask;
[X, Y] = find(separation);
imshow(mask); hold on; plot(Y, X, '.r');
Each element in X has a corresponding element in Y. I need to remove the repeating values in X and also their correspondent values in Y without rearranging the order of elements. I'm using this code, but it does not remove the duplicating elements. What is the problem?
A=[X,Y];
[UniXY,Index]=unique(A,'rows','first');
DupIndex=setdiff(1:size(A,1),Index);
A(DupIndex,:)=[];
X=A(:,1);
Y=A(:,2);
figure; imshow(mask); hold on; plot(Y, X, '.r');
Any help is appreciated.
Upvotes: 0
Views: 1635
Reputation: 2826
You could try applying unique
with the 'stable'
argument on X, and using the indices extracted to extract the corresponding values of Y.
% 'stable' argument preserves ordering
[Xfixed, ind] = unique(X, 'stable');
% ind now holds the indices of the unique elements
YFixed = Y(ind);
In your code, calling unique(A, 'rows', 'first')
will not preserve ordering of elements. It will also not remove duplicate values of X if their corresponding Y values are different. However, if 2 identical X values always correspond to 2 identical Y values, what you are asking for can be accomplished simply by
A = unique([X Y], 'rows', 'stable');
Upvotes: 1