How to find the same multiple elements in two different matrices

3 次查看(过去 30 天)
Hi! Personally I am new comer for using MATLAB.
So please pardon me if my question seems easy to you.
I have two matrices
Matrix A = X Y Z S; %XYZ is the coordinate and S is the slope values
Matrix B = X Y Z D %XYZ is also the coordinate and D is the displacement values
This aims to find the same XYZ between matrix A and matrix B so that I can overlay the slope and displacement values within the same XYZ.
said matrix_A =
684985 3640891.9998 606.3228 20.95918
684985 3640890.9998 606.3158 24.53832
684985 3640889.9998 606.2890 21.29358
684985 3640888.9998 606.2283 17.91158
684985 3640887.9998 606.0254 18.56814
684985 3640886.9998 605.7449 21.54986
684985 3640885.9998 605.5593 26.87469
684985 3640884.9998 605.5601 29.04047
684985 3640883.9998 605.5554 28.95754
684985 3640882.9998 605.0802 40.39104
684985 3640881.9998 603.6958 49.32053
684985 3640880.9998 603.6613 50.2536
684985 3640879.9998 605.6673 45.09067
684985 3640878.9998 606.2387 32.24639
684985 3640877.9998 606.1561 38.44921
684985 3640876.9998 605.9392 41.90742
684985 3640875.9998 605.6975 42.67643
684985 3640874.9998 605.4557 42.75308
684985 3640873.9998 605.2139 42.64434
684985 3640872.9998 604.9722 42.50702
and matrix_B =
685455 3640402.1594 569.144 -0.0287443747909282
685448 3640392.7359 570.522 -0.0287443747909282
685448 3640393.0711 570.988 -0.0287443747909282
685452 3640399.7078 569.736 -0.0287443747909282
685438 3640411.0395 579.175 -0.0287443747909282
684985 3640886.9998 605.7449 0.00655349653231637
684985 3640885.9998 605.5593 0.00655349653231637
684985 3640884.9998 605.5601 0.00655349653231637
684985 3640883.9998 605.5554 0.00655349653231637
684985 3640882.9998 605.0802 0.00655349653231637
684985 3640881.9998 603.6958 0.00655349653231637
684985 3640880.9998 603.6613 0.00655349653231637
684985 3640879.9998 605.6673 0.00655349653231637
684985 3640878.9998 606.2387 0.00655349653231637
684985 3640877.9998 606.1561 0.00655349653231637
I have huge matrix size, not only this simple example. Also, matrix A is larger than matrix B, since it represents the whole area, while matrix B is just a part of it. I am sorry if my explanation is a bit messy.
I have tried to use:
inputA = matrix_A(:,1:3);
inputB = matrix_B(:,1:3);
[iC,ia,ib] = intersect(inputA, inputB);
dummyan = [dummyclean(ia,1:4) Disp(ib,4)];
but it got me an error.
Hopefully someone can understand and help me.
Thank in advance!

回答(2 个)

KSSV
KSSV 2019-6-4
编辑:KSSV 2019-6-4
REad about ismember. Let A and B be your matrices.
[idx,ia] = ismember(A(:,1:3),B(:,1:3),'rows') ;
A(idx,:)
B(idx,:)
  2 个评论
DV
DV 2019-6-4
Hi! Thank you for the answer.
I tried this too, but I want to get an exactly the same of XYZ between two matrices. Sorry for my unclear explanation.

请先登录,再进行评论。


Stephen23
Stephen23 2019-6-4
编辑:Stephen23 2019-6-4
Do NOT use ismember! Because of the floating-point numbers you should use ismembertol:
>> [idx,idy] = ismembertol(B(:,1:3),A(:,1:3),'ByRows',true)
>> C = [A(idx,:),B(idy(idx),4)]
C =
6.8498e+05 3.6409e+06 6.0574e+02 2.1550e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0556e+02 2.6875e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0556e+02 2.9040e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0556e+02 2.8958e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0508e+02 4.0391e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0370e+02 4.9321e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0366e+02 5.0254e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0567e+02 4.5091e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0624e+02 3.2246e+01 6.5535e-03
6.8498e+05 3.6409e+06 6.0616e+02 3.8449e+01 6.5535e-03

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by