# Is there any way to find the row with closest values in matrix with a given array values?

3 views (last 30 days)
SHUBHAM AGARWAL on 4 Oct 2022
Commented: SHUBHAM AGARWAL on 4 Oct 2022
Dear expert,
I have an array (1mat.txt) and I want to check the closest row in the matrix (2mat.txt) with the array. In this 2mat.txt i have 5 rows (which can be seen with graph) but in principle I have bigger matrix like 50 rows or 100 rows. Kindly help me with this.
Thank you

Torsten on 4 Oct 2022
Edited: Torsten on 4 Oct 2022
How do you define "closest" ? The Euclidean distance between array and rows of the matrix ?
Then you can use
mat1 = [1.0000 0.1803 0.0779 0.0475 0.0369 0.0269 0.0208 0.0166 0.0093 0 0.0009 0.0008];
mat2 = [1 0 0 0 0 0 0 0 0 0 0 0
1.0000 0.1579 0.0244 0.0033 0 0 0.0003 0 0 0 0 0
1.0000 0.4389 0.1907 0.0842 0.0332 0.0169 0.0061 0.0049 0.0019 0 0 0
1.0000 0.5587 0.3522 0.2150 0.1275 0.0599 0.0423 0.0275 0.0099 0.0041 0.0038 0
1.0000 0.5728 0.3655 0.2707 0.1718 0.1064 0.0629 0.0337 0.0184 0.0132 0 0.0010];
[~,i] = min(vecnorm((mat2-mat1).'))
i = 2
SHUBHAM AGARWAL on 4 Oct 2022
Dear Torsten,
Thanks for the explanation of this method. It works.
Thank you very much.

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by