Borrar filtros
Borrar filtros

Finding the equal elements in 2 matrices

4 visualizaciones (últimos 30 días)
Salar
Salar el 25 de Feb. de 2016
Comentada: Salar el 25 de Feb. de 2016
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!

Respuesta aceptada

Stephen23
Stephen23 el 25 de Feb. de 2016
Editada: Stephen23 el 25 de Feb. de 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 comentarios
Stephen23
Stephen23 el 25 de Feb. de 2016
Editada: Stephen23 el 25 de Feb. de 2016
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
Salar
Salar el 25 de Feb. de 2016
oh ok , thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by