Get row and column of the closest value of a matrix from a variable
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a latitude vector of dimensions 17 x 1. I have a latitude matrix of dimension 300 x 300.
I would like to find the row and column of the closest value of the matrix for each one of the vector's values. I tried several codes but none of them are working.
How can I solve this problem ?
Thanks in advance !
0 个评论
回答(1 个)
Adam
2020-3-10
编辑:Adam
2020-3-10
If you are using an up to date version of Matlab (scanning the release notes I think R2019a or later for the particular syntax of min used here) this should work, although I don't have time to fully test it:
a = rand(17,1);
b = rand(300);
[~, idx] = min( abs( b - reshape( a, 1, 1, [] ) ), [], [1 2], 'linear' );
[row,col] = ind2sub( [300, 300], squeeze( idx ) );
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!