Get row and column of the closest value of a matrix from a variable

조회 수: 2 (최근 30일)
V.D-C
V.D-C 2020년 3월 10일
편집: V.D-C 2020년 3월 11일
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 !

답변 (1개)

Adam
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 ) );
  댓글 수: 1
V.D-C
V.D-C 2020년 3월 11일
편집: V.D-C 2020년 3월 11일
Thank you for your answer ! The row vector looks realistic but the col vector isn't: the values grow along the vector to reach more than 5000 which is not supposed to be possible. I tried with my data and also random a and b and still got erratic values for columns.
ps: I am using 2019b

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by