Question

I have two sets of data. One set of data is a matrix containing different samples in each row and information regarding each sample in the columns, one of these columns contains longitude data and another one contains latitude data for the sample. The other dataset consists of three grids. One grid contains the latitude of the data, the second grid contains the longitude of the data and the third grid, the data for the 1° latitude longitude grid.

What I would like is to find out which data in the second dataset corresponds with the data in the second dataset. What I mean by this, is if a sample falls into a particular grid of the second dataset, the data in this grid needs to be extracted and it needs to be known for what sample the data applies.

So just say in the grid between latitudes 60 and 59, and longitudes 100 and 101 sample x falls. Just say the data in the gridded dataset is 10 for this particular grid. I would like to know that 10 (the data in the grid) applies to sample x.

In the end I would like to have the grid data that corresponds to a sample in a new matrix that could act as a partner to the sample dataset (ie. if sample x is in row 40 then in the matrix 10 is in row 40), or alternatively added to the same dataset as a new column. Keeping in mind that some samples will fall into the same grid.

I'm fairly inexperienced in matlab, I have tried the brushing tool but this does not work for this example. All I could think of that could potentially work is round each long and lat in the sample data to an even number and then find which samples overlap in long and lat and then intersect the long in the sample data with the long grid and then do the same for the lat grid finding the row and column each sample falls into and then find the data for each sample. This seems like a long way to go about it and I'm not too sure how well it would work as well.

I have completed this method and it has worked to an extent.... I have the rows and columns in which the data is for each sample (ie. sample x can be found in row 8 column 100). However when I try and extract this data from the grid it is not a matrix containing one column but many columns, the answer is still in the sample place of the matrix. How do I go about taking one data point from each row of a grid and ending up with a matrix of only one column (or one row in which I can turn into a column)?

Thank you

No correct solution

OTHER TIPS

Presuming your first data set is in a matrix X, where the first column is latitude and the second is longitude and any other columns are existing data:

xlat = floor(X(1,:)); 
xlon = floor(X(2,:));

lat = % a list of the latitudes covered in the grid
lon = % a list of the longitudes covered in the grid
data = % the matrix of data you want to extract - a 2D grid

Such that lat(n),lon(m) together forms a reference to the left-bottom corner of the grid square, which contains data in data(n,m). floor rounds down, so that anything between 100 and 101 will be linked to 100, etc.

Now:

   [~ n] = ismember(xlat,lat);
   [~ m] = ismember(xlon, lon);

n and m are not latitudes or longitudes but indexes which relate the points in your data set X to some values in lat and lon which then refer to some value in the grid data.

Here's the last trick - use sub2ind to convert your n and m references to a single reference to the position in the grid, then extract all the required data in one go:

ind = sub2ind(size(data),n,m); % presuming the size of data is lat x lon);
Xdata = data(ind);

Xdata should be a single column, with the same size as the number of rows in X.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top