How can I keep the rows where the elements of 1st column are in 2nd column

I have a edge list as below:
1 2
1 3
1 9
2 1
2 3
3 1
3 2
3 4
3 9
I need in matlab rows where 1st column values are present in 2nd column. i.e. From the above I need rows in which 1st column values(1,2,3) are present in 2nd column(i.e. keep the rows where 2nd column too has values 1,2,3 and the rest remove. So finally it should be:
1 2
1 3
2 1
2 3
3 1
3 2

Answers (1)

M=[1 2
1 3
1 9
2 1
2 3
3 1
3 2
3 4
3 9];
Mpc = M(ismember(M(:,2),unique(M(:,1))),:)

This question is closed.

Asked:

on 1 Jul 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!