Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matlab - Compare 2 array values 1

Status
Not open for further replies.

TLamb

Structural
Nov 21, 2002
9
0
0
GB
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.
 
Replies continue below

Recommended for you

Code:
% assuming that your arrays are in the form
% 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,:)]
M

--
Dr Michael F Platten
 
Status
Not open for further replies.
Back
Top