How to find the row numbers for the pairwise entries from one matrices to another?

2 views (last 30 days)
I am trying to compare two matrices of length 2xn and 2xm for n>m. For example consider n=19,m=16. Then we have the following two vectors, nodes (2x19) and old_nodes (2x16).
%Nodes Old_nodes
0 0 0 0
0 0.333333333 0 0.333333333
0 0.666666667 0 0.666666667
0 1 0 1
0.333333333 0 0.333333333 0
0.333333333 0.333333333 0.333333333 0.333333333
0.333333333 0.666666667 0.333333333 0.666666667
0.333333333 1 0.333333333 1
0.666666667 1 0.666666667 0
1 0.666666667 0.666666667 0.333333333
1 1 0.666666667 0.666666667
1 0.166666667 0.666666667 1
0.666666667 0 1 0
1 0 1 0.333333333
0.833333333 0.166666667 1 0.666666667
0.666666667 0.666666667 1 0
0.666666667 0.333333333
1 0.333333333
0.833333333 0.333333333
From here I am then trying to find out which rows in nodes are in old_nodes, there should be 16 of them. However, my attempt does not yield any effective results. I have tried the following, by taking four elements that i know are in old_nodes (rows: 6 7 10 11)
j=0
r=0
old_free=[6 7 10 11]
rows=zeros(length(old_free),1);
for i=1:length(e)
% i
for jj=1:length(old_free)
[row,~]=find(nodes(i,1)==old_nodes(old_free(jj),1) & nodes(i,2)==old_nodes(old_free(jj),2))
if isempty(row)
r=r+1
else
j=j+1
rows(j)=row;
end
end
end
Essentially what i would like is a column with the indices of the coordinates from nodes which match the indices of the entries for old_nodes, which will all be unique pairs:
e.g.
1 4
5 8
11 3
If anyone can be of help, I'd greatly appreciate it.

Accepted Answer

Mehdi Ben Slama
Mehdi Ben Slama on 4 Jun 2019
Hi, it looks to me that the function 'ismember' does what you want. Specifically:
ismember(nodes, old_nodes, 'rows')
In this case, you're interested not only in whether the rows of 'nodes' are contained in 'old_nodes', but also their location in 'old_nodes'. You can obtain both in this manner:
[ LIA, LOCB ] = ismember(nodes, old_nodes, 'rows')
LIA is a vector with values 1 or 0 depending on whether the row of A is a member of B.
LOCB is a vector containing the location (row index) in B when a row in A is a member of B, 0 otherwise.
You should be able to get what you need by fiddling around with LOCB to get it into your desired format!
PS: do check the help section on 'ismember', it's probably more exhaustive

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!