Doubt about finding the nearest values between array and a matrix
Show older comments
Hi, everybody
I'm trying to find the nearest values from a array inside a matrix. Example:
Array = [12, 16, 300, 200, 250, 380, 145, 6, 8, 10, 12, 100, 16, 440]; %array with 14 elements
Matrix = [15, 20, 310; 1000, 500, 1; 146, 10, 11; 5, 23, 800; 600, 100, 50]; %matrix 5x3
but when a try:
for i=1:size(Array,2)
[~,ID] = min(abs(Matrix - Array(i)),[],1);
end
The ID have just 3 elements, not 14. I don't understand why. Anybody could explain to me. please.
Att,
Paulo
Accepted Answer
More Answers (1)
Jon
on 25 Jan 2022
1 vote
You are overwriting the value of ID with each pass through your loop, so you only see the value for the last pass.
If you want the value for every pass then you need to assign ID(i) in the loop
5 Comments
Also, I'm not really clear on what you are looking for here. Do you want to know for each element of Array the value in Matrix (doesn't matter the row or column) which is the closest?
So for example for Array(1) = 12, what is the answer you are hoping to find?
DGM
on 25 Jan 2022
I'm just going to add that clearing up this confusion would be helped greatly by using meaningful variable names. Calling an array "Array" is about as fruitful as naming your kids "Hey You".
Paulo Cunha
on 25 Jan 2022
Edited: Paulo Cunha
on 26 Jan 2022
Paulo Cunha
on 25 Jan 2022
Paulo Cunha
on 26 Jan 2022
Categories
Find more on Matrix Indexing 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!