Combining two vectors into a new one
Show older comments
Hi,
I am working on a dynamic programming problem and I now have two equal-sized vectors, say vector A and B. I am trying to generate a new vector that starts with the first element of vector B, then finds the closest value in A and takes it's index value, then records the value corresponding to this index in B. I want to make a loop and cover all values in my vector B. Is there a way to code this? (Vector A and B won't have the exact same values unfortunately, so the code will need to find the closest element's index)
Thanks!
Accepted Answer
More Answers (1)
KSSV
on 11 Mar 2022
A = rand(3) ;
B = rand(2) ;
idx = knnsearch(B(:),A(:))
Also have a look on ismember, ismembertol.
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!