Finding the equal elements in 2 matrices

Hello,
I've been looking online for a code that would help me with this, but I haven't found anything useful yet. I hope you can help me with this ! Let's say we have 2 matrices: Important point is that it IS guaranteed that there is 1 and only 1 element in each row of a & b that are the same. for example in row 1, 2 is the same, and in row 2, 3 is the same in this case. The column index of the elements are not necessarily the same. for example in row 1, the element "2" is in the 2nd column, but in b "2" is the first column. I need a code that would extract all these values and return a column vector with of all the common values, and this column vector MUST be in the same order as a and b. so basically in this case I want this code to give me : FYI, I have looked into ismember/ intersect functions already!
a =
1 2
3 4
b =
2 5
3 0
C =
2
3
I also have this code, which makes sense to me mathematically, but I idk why it's not giving me the correct answer :
tf = abs(a-b) < 1e-1 | abs(fliplr(a)-b) < 1e-1;
C = a(tf);
Thank you for your time!

 Accepted Answer

Stephen23
Stephen23 on 25 Feb 2016
Edited: Stephen23 on 25 Feb 2016
You need to take into account that MATLAB operates column-wise by transposing your matrices:
>> a = [90,50;20,11;0,87;11,110]
>> b = [41,90;11,31;87,1;11,-2]
>> at = a.';
>> bt = b.';
>> xt = at==bt | at==bt([2,1],:)
>> at(xt)
ans =
90
11
87
11
This is your example data form your earlier comments. Please do not post the same question in multiple locations, it makes it vary hard for us to keep track of what information you have been given. Of course the examples you give here also work:
>> a = [1,2;3,4];
>> b = [2,5;3,0];
>> at = a.';
>> bt = b.';
>> xt = at==bt | at==bt([2,1],:);
>> at(xt)
ans =
2
3
Or using your method with a tolerance:
>> tf = abs(at-bt) < 1e-1 | abs(at-bt([2,1],:)) < 1e-1;
>> at(tf)
ans =
2
3
Note in all cases I used only the transposed matrices, because MATLAB operates columns-wise.

13 Comments

Thank you! I'm sorry I just thought I'll get more views making a new thread. Thank you !
Stephen,
Sorry to bother again, but I have another question. so the vector that this is giving me has less columns than my original matrices. Do you know why this happens and how can I fix it? I really appreciate you taking the time.
LA=[A B];
LB=[C D];
L1 = LA.';
L2 = LB.';
LL = L1==L2 | L1==L2([2,1],:);
G=L1(LL);
All L matrices have 14001 rows. but G has 11532 rows.
I guess there are some rows without matching values. Try this:
find(~any(LL,2))
to get the rows without any matching elements.
It's weird. it's giving me this hahaha
Empty matrix: 0-by-1
try this:
[R,C] = find(all(LL==false,2))
If they are also empty then can you please upload those variables in a file, and I will take a look.
They were also empty. M files are attached. Thank you so much!
You did not provide the function AAE590_HW5, which is used in the ode45 call.
It might be easier if you simply put LA and LB into a mat file and upload it here. (add ".txt" to its filename).
I uploaded 2 m files. one is the script and the other is the function. I can see them here. I'll do that.
I just uploaded LA and LB
The two files you uploaded have exactly the same data in them:
>> LA = load('LA.m','txt');
>> LB = load('LB.m','txt');
>> isequal(LA,LB)
ans =
1
Each has 5307 rows, and the output has 10614 elements. This means the code is performing as it should.
I'm so sorry. I copied pasted the same one. here you go this is LB.
There are 870 rows without any matching values. This will locate them:
>> find(~any(LL,1))
ans =
Columns 1 through 8
1725 1726 1727 1728 1729 1730 1731 1732
Columns 9 through 16
1733 1734 1735 1736 1737 1738 1739 1740
... lots more here
Columns 857 through 864
4454 4455 4456 4457 4458 4459 4460 4461
Columns 865 through 870
4462 4463 4464 4465 4466 4467
>> numel(ans)
ans =
870
oh ok , thank you!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 25 Feb 2016

Commented:

on 25 Feb 2016

Community Treasure Hunt

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

Start Hunting!