Thread Subject: Deleting row if entry appeared before

Subject: Deleting row if entry appeared before

From: Alex

Date: 10 Nov, 2009 04:16:02

Message: 1 of 7

I'm trying to delete rows (edges in a graph) in an n by 2 matrix if any of the row's entries appear in a previous row.

For example:
adjtri =
     1 2
     3 2
     3 4
     6 1
     5 6
     8 3
     7 6
     7 8

Row 2 must be deleted because the 2 appears in row 1.
New adjtri is adjtri(2,:) = [];
Then in the new array, row 5 must be deleted because the 3 appears in row 2 of the new matrix and so on until adjtri is (one solution of many):
adjtri =
     1 2
     3 4
     5 6
     7 8

adjtri is a list of adjacent triangles in a delaunay triangulation. What I want is to break the delaunay triangulation into non overlaping quadrilaterals composed of 2 triangles.

Here is a solution I came up with but I'm not sure it will work all the time:
(I'm having some concerns about the if statement; what if it's > 2?)

adjSize = size(adjtri,1);
for n = 1:adjSize
    holder = ismember(adjtri,n);
    if sum(holder(:,1) + holder(:,2)) > 1
        delete = max((holder(:,1) + holder(:,2)).*(1:size(adjtri,1))');
        adjtri(delete,:) = [];
    end
end

Thank you for your time.
Alex.

Subject: Deleting row if entry appeared before

From: Jos

Date: 10 Nov, 2009 07:34:07

Message: 2 of 7

"Alex " <alexmbcm@yahoo.com> wrote in message <hdape2$ofu$1@fred.mathworks.com>...
> I'm trying to delete rows (edges in a graph) in an n by 2 matrix if any of the row's entries appear in a previous row.
>
> For example:
> adjtri =
> 1 2
> 3 2
> 3 4
> 6 1
> 5 6
> 8 3
> 7 6
> 7 8
>
> Row 2 must be deleted because the 2 appears in row 1.
> New adjtri is adjtri(2,:) = [];
> Then in the new array, row 5 must be deleted because the 3 appears in row 2 of the new matrix and so on until adjtri is (one solution of many):
> adjtri =
> 1 2
> 3 4
> 5 6
> 7 8
>
> adjtri is a list of adjacent triangles in a delaunay triangulation. What I want is to break the delaunay triangulation into non overlaping quadrilaterals composed of 2 triangles.
>
> Here is a solution I came up with but I'm not sure it will work all the time:
> (I'm having some concerns about the if statement; what if it's > 2?)
>
> adjSize = size(adjtri,1);
> for n = 1:adjSize
> holder = ismember(adjtri,n);
> if sum(holder(:,1) + holder(:,2)) > 1
> delete = max((holder(:,1) + holder(:,2)).*(1:size(adjtri,1))');
> adjtri(delete,:) = [];
> end
> end
>
> Thank you for your time.
> Alex.

help unique

and take a look at the rows option

hth
Jos

Subject: Deleting row if entry appeared before

From: Alex

Date: 11 Nov, 2009 03:00:19

Message: 3 of 7

Thanks for the prompt response Jos.
I've been trying to use the unique function with the 'rows' option and it still doesn't do what I want to do without using for loops. I could be great if it had a row entry option.

Subject: Deleting row if entry appeared before

From: Pekka Kumpulainen

Date: 11 Nov, 2009 07:01:05

Message: 4 of 7

"Alex " <alexmbcm@yahoo.com> wrote in message <hdd9c3$8u2$1@fred.mathworks.com>...
> Thanks for the prompt response Jos.
> I've been trying to use the unique function with the 'rows' option and it still doesn't do what I want to do without using for loops. I could be great if it had a row entry option.

unique with 'rows' only removes the rows where all elements are equal? You want to remove the row if any of the elements equals the one in the row above?
doc diff
doc any
something like this perhaps:
adjtri([false; any(~diff(adjtri),2)],:) = [];

Subject: Deleting row if entry appeared before

From: Alex

Date: 13 Nov, 2009 03:32:02

Message: 5 of 7

Thanks for the response Pekka;
   Actually I want to delete any rows that share entries. For example, if the original array is this:
1 2
3 2
3 4
6 1
5 6
8 3
7 6
7 8

One of the answers is:
1 2
3 4
5 6
7 8
(That they are consecutive numbers is a coincidence)
Or:
6 1
3 4
7 8

In terms of graphs, I want to disconnect all edges by deleting some of them. What I'm using this for is to break a delaunay triangulation into nonoverlapping quadrilaterals. The example I'm using is a square broken down into 4 equal parts with a diagonal ( / ) in each. If I name the triangles like this:
5 6 7 8
1 2 3 4
The original array is a list of triangles that share an edge. Then triangle pairs 1 2 and
3 2 share a triangle, so the quadrilaterals formed by them overlap.
 
I took a closer look at my original code and I think it works for all cases. This might also help with the explanation:
for n = 1:size(adjtri,1)
    holder = ismember(adjtri,n);
    if sum(holder(:,1) + holder(:,2)) > 1
       delete = max((holder(:,1) + holder(:,2)).*(1:size(adjtri,1))');
       adjtri(delete,:) = [];
    end

Do you have a way to do this or vectorize the above code?

Thanks.
Alex.

Subject: Deleting row if entry appeared before

From: Matt Fig

Date: 14 Nov, 2009 00:28:02

Message: 6 of 7

This should be faster than the code you posted, if I understand the problem correctly. My solution is not vectorized, but on my machine (Xeon quad core 64 bit) it is 5-6 times faster.


U = unique(adjtri3);
R = size(adjtri3,1);
T = true(R,1);
for ii = 1:R
    if adjtri3(ii,1)>adjtri3(ii,2)
        R = [2,1];
    else
        R = [1,2];
    end
    TF = ismembc(U,adjtri3(ii,R)); % Second arg must be sorted.
    if nnz(TF)==2
        U(logical(TF)) = [];
    else
        T(ii) = 0;
    end
end
adjtri3 = adjtri3(T,:);

Subject: Deleting row if entry appeared before

From: Alex

Date: 14 Nov, 2009 02:02:02

Message: 7 of 7

Thanks for the help Matt

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
unique Alex 12 Nov, 2009 22:34:26
delaunay Alex 12 Nov, 2009 22:34:26
mesh Alex 12 Nov, 2009 22:34:26
triagulation Alex 12 Nov, 2009 22:34:26
graph theory Alex 12 Nov, 2009 22:34:25
rssFeed for this Thread

Contact us at files@mathworks.com