Finding rows and columns of a data set

2 views (last 30 days)
My problem is this. I have two 2d matrices, one contains latitude coordinates, the other contains longitude coordinates so that the same elements in each matrix represent a coordinate on a map, for example (18,-161). How do I find the location of the element (ie what row and what column) where I can find any specific coordinate I am looking for? If I wanted to find the exact row and column where 18 and -161 match up, how would I do that? I should also note that the rows of the latitude matrix and the columns of the longitude matrix are not homogenous, that is, they do not all have the same values.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Mar 2012
Are the two matrices the same size?
llidx = find(lat == 18 && long == -161);
if length(llidx) ~= 1
fprintf('%d matches instead of one\n', length(llidx));
else
[row, col] = ind2sub(size(lat), llidx);
end
However! See http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F on why you should probably not use equality matches.
If the coordinates you are searching for might be only approximately in the matrices, and you want to find the closest point, then another approach would have to be used, such as interp2() on unique() of lat and unique(long), using the 'nearest' option of interp2(). Though there are other good ways if taxi-cab distance is okay instead of Euclidean to determine the "closest" point.
  1 Comment
Andrew
Andrew on 17 Mar 2012
The matrices are both the same size. Yeah the data is in decimals so there never is exactly the integer 18 or -161 to find, it would always be between two elements. On the plus side, I don't need to find the precision values. I just need the function to find approximate values so I can crop out the parts of each matrix I don't need. Ultimately I will be using the griddata function, together with the latitude and longitude matrices to interpolate another matrix that contains scientific data to a new latitude and longitude grid. Unfortunately, griddata is very memory intensive and my computer maxes out before the function is completed. By cropping out the data I don't need early on, I'm hoping to save some memory.

Sign in to comment.

More Answers (0)

Categories

Find more on Geographic Plots 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!