# How to compare the closest value in the matrix?

2 views (last 30 days)
rupak katwal on 30 Oct 2019
Answered: Fabio Freschi on 31 Oct 2019
I have the three vectors from the reference values of one vector, i have to choose the closet value among other two vector?
suppose
if R is the reference vector
unknownM =0.8105 1.1581 0.6295 0.9594 0.8447 0.7103 0.8429 0.5372 0.9882 0.8924
then from the other two vector
sudoM =0.7768 1.1883 0.8918 0.6824 1.1393 0.6507 1.3433 1.2419 0.7456 1.1048
grepM = 0.8034 0.7853 1.0771 0.6649 0.5639 0.8384 0.6207 0.6000 0.6896 0.8011
for the first value of unkownM it should select the value of grepM an continous.
##### 2 CommentsShowHide 1 older comment
Adam Danz on 31 Oct 2019
The question isn't clear. As darova suggestion, some input/output pairs might be helpful in addition to another explanation of the problem.

Fabio Freschi on 31 Oct 2019
unknownM = [0.8105 1.1581 0.6295 0.9594 0.8447 0.7103 0.8429 0.5372 0.9882 0.8924];
sudoM = [0.7768 1.1883 0.8918 0.6824 1.1393 0.6507 1.3433 1.2419 0.7456 1.1048];
grepM = [0.8034 0.7853 1.0771 0.6649 0.5639 0.8384 0.6207 0.6000 0.6896 0.8011];
% preallocation
closest = zeros(size(unknownM));
% errors
errSudoM = abs(sudoM-unknownM);
errGrepM = abs(grepM-unknownM);
% sudoM is closer
idx1 = errSudoM < errGrepM;
closest(idx1) = sudoM(idx1);
% grepM is closer
closest(~idx1) = grepM(~idx1);

### Categories

Find more on Entering Commands in Help Center and File Exchange

### Tags

No tags entered yet.

### Community Treasure Hunt

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

Start Hunting!

Translated by