Solved
%%X coordinate
[~, indX] = unique(B(:, 2), 'rows');
% duplicate indices
duplicate_indX = setdiff(1:size(B, 1), indX);
% duplicate values
duplicate_valueX = B(duplicate_indX, 2);
%%Y coordinate
[~, indY] = unique(B(:, 3), 'rows');
% duplicate indices
duplicate_indY = setdiff(1:size(B, 1), indY);
% duplicate values
duplicate_valueY = B(duplicate_indY, 3);
%%Both coordinates
duplicate_ind=intersect(duplicate_indX,duplicate_indY);
duplicate_value = B(duplicate_ind, 2:3);
indexes=zeros(3,size(duplicate_value,1));
for i=1:size(duplicate_value,1)
if size(find(B(:,2)==duplicate_value(i,1)&B(:,3)==duplicate_value(i,2)),1)==2
indexes(1:2,i)=find(B(:,2)==duplicate_value(i,1)&B(:,3)==duplicate_value(i,2));
end
if size(find(B(:,2)==duplicate_value(i,1)&B(:,3)==duplicate_value(i,2)),1)==3
indexes(1:3,i)=find(B(:,2)==duplicate_value(i,1)&B(:,3)==duplicate_value(i,2));
end
end
for j=1:size(B,1)-1
lines(j,1:2)=j;
lines(j,3)=j+1;
end
for j=1:size(lines,1)
for i=1:1:size(indexes,2)
if indexes(3,i)==0
if lines(j,2)==indexes(2,i)
lines(j,2)=indexes(1,i);
end
if lines(j,3)==indexes(2,i)
lines(j,3)=indexes(1,i);
end
end
if indexes(3,i)~=0
if lines(j,2)==indexes(3,i)
lines(j,2)=indexes(1,i);
end
if lines(j,3)==indexes(3,i)
lines(j,3)=indexes(1,i);
end
if lines(j,2)==indexes(2,i)
lines(j,2)=indexes(1,i);
end
if lines(j,3)==indexes(2,i)
lines(j,3)=indexes(1,i);
end
end
end
end