Trying to link two matrices based on 2 columns in each

7 views (last 30 days)
I have two matrices involving the data of stars that I am trying to link by their x and y coordinates in MATLAB. The first matrix is 245443x27 and the second is 53432x12 and by the end of this I'm hoping for a data set that is 53432x39. The matrices are not ordered the same and obviously the do not contain the same data.
The x and y coordinates are located in the 2nd and 3rd column in the large matrix and in the first and second columns in the small matrix.
I am not that good at coding, so i cant seem to get it to work. So far I have tried using a for loop with an if statement trying to take an x value from one, and search through the other to find the same, then take the y value of the first and see if it matches. If it doesn't then it repeats until it goes through the whole data set.
Thank you in advance!
  1 Comment
KSSV
KSSV on 11 Sep 2017
Instead of sorting them..you can go for interpolation, which could give you what you want.

Sign in to comment.

Answers (1)

OCDER
OCDER on 12 Sep 2017
Edited: OCDER on 12 Sep 2017
Try using intersect.
%A represents the large matrix. Col 2 and 3 are the x and y coordinates, respectively.
A = [1 2 3;
2 4 6;
3 3 2;
4 6 8];
%B represents the small matrix. Col 1 and 2 are the x and y coordinates, respectively.
B = [6 8 1;
3 2 2];
[~, IA, IB] = intersect(A(:, 2:3), B(:, 1:2), 'rows');
AB = [A(IA, :) B(IB, :)]; %This is the combined matrix (the 39-column one)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!