Matlab - Compare 2 array values
Matlab - Compare 2 array values
(OP)
I have two arrays holding coordinates of the cell centers for a finite volume program. One array has 100 x and y values whilst the other has 16 values. I would like to match the cell centers that have the same x and y coordinates in the 2 arrays. Currently I am able to do this using for loops but it takes forever to run through the values for larger arrays.
Can anyoe give advice on how this process can be simplified using matlabs built in matrix functions?
I eagerly await any suggestions.
Can anyoe give advice on how this process can be simplified using matlabs built in matrix functions?
I eagerly await any suggestions.





RE: Matlab - Compare 2 array values
CODE
% A = x1 y1 and B = X1 Y1
% x2 y2 X2 Y2
% : : : :
% x100 y100 x16 y16
%
% i.e. rows of x-y pairs (not necessarily 100 or 16 rows)
% Generate a test matrix of the form A above
A = rand(100,2)
% Generate a test matrix of the form B above
% (some of whose members are the same as A)
B = [ A([1 16 29 43 32],:)
1.2 3.4
55.4 60.3
A([12 3 4 99 84],:)
34.5 56.7
A([66 78 36],:) ];
% find the set-wise intersection between the two datasets
% and the corresponding indices
[C, A_idx, B_idx] = intersect(A, B, 'rows');
% C contains the matching x-y pairs
% A_idx contains the row numbers of A corresponding to the
% matching pairs
% B_idx contains the row numbers of B corresponding to the
% matching pairs
% such that C == A(A_idx, :) == B(B_idx, :)
[C, A(A_idx,:), B(B_idx,:)]
--
Dr Michael F Platten
RE: Matlab - Compare 2 array values
RE: Matlab - Compare 2 array values