Finding rows and columns of a data set
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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.
More Answers (0)
See Also
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!