user2877623
user2877623

Reputation: 35

Extracting data from a matrix based on second longitude and latitude matrix in MATLAB

I have two matrices, one contains data about samples collected (each row represents a sample and the columns different pieces of information for that sample e.g. longitude and latitude where it was collected) and the other contains particular points of interest (one column contains latitude and the other longitude with each row being a point of interest).

For example points of interest may be:

[-64.7664,
 -64.2296,
 -64.4314,
 -64.4869,
 -64.400,
 -64.4000;

 117.3117,
 127.9522,
 127.9867,
 128.10270,
 127.13330,
 127.13330]

Where column one represents latitude and column 2 represents longitude.

Whereas the sample data will look something like:

c1   c2   c3   c4   c5        c6        c7    ...
0,   3,   6,   4,   -74.21,   103,      4     ...
1,   NaN, 4,   6,   -70.1,    101,      8     ...
3,   0,   5,   3,   -64.7664, 117.3117, 5     ...
2,   4,   12,  NaN, -62.1,    120,      NaN   ...
19,  5,   16,  1,   -64.400,  127.1333, 16    ...

Data continues for another 29 columns. In this example, row three, and 5 would be needed to be made into a new matrix.

I need to find which samples are at these particular interest points (so the longitude and latitude of the sample is the same as one of the rows of the data containing the particular points of interest).

How do I go about finding which samples (rows) were collected at the points of interest and then create a new matrix containing only the information of the samples found at the points of interest?

Upvotes: 1

Views: 136

Answers (1)

marsei
marsei

Reputation: 7751

You can use intersect like in this example

a = [1 2; 3 4; 5 5];
b = [3 4; 5 5; 2 1];

[c,ia,ib] = intersect(a,b,'rows')

where the vectors ia contains the common indexes for a and ib the common indexes in b.

Upvotes: 0

Related Questions