How to identify the approximate value from two different vectors?

1 次查看(过去 30 天)
Hi, I have two different vectors where
A(Ref)=[14.553 14.100 90.4512 90.901]
B= [12.34 14.120 14.440 13.59 90.419 90.850 300.123]
I want to identify the approximate value of the A(Ref) vector from B vector. Can anybody please help me how to identify the A(Ref) values from B vector(approximate/closest)?
Output result should be
ans = [14.120 14.440 90.419 90.850]
from B
  2 个评论
James Tursa
James Tursa 2015-9-10
How did you get your result? If I was looking for the closest match in B to the first value in A (14.553) I would have guessed your result would be 14.440. But you list 14.120. Is this a typo? Same comment for the 2nd element.
dpb
dpb 2015-9-10
Perhaps the output is sorted in ascending order, James. That'd select the values for the first two but rearrange them.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2015-9-10
I agree with James that your output does not match your description. But see if this does what you want:
ARef = [14.553 14.100 90.4512 90.901];
B = [12.34 14.120 14.440 13.59 90.419 90.850 300.123];
for k = 1 : length(ARef)
[minDistance, indexOfMinDistance] = min(abs(ARef(k) - B));
out(k) = B(indexOfMinDistance);
end
% Show in command window:
out
Result:
out = 14.44 14.12 90.419 90.85
  2 个评论
D Bhuiyan
D Bhuiyan 2015-9-10
Thanks for your feedback. Here I did a mistake, actually I would like to see: If two vectors are like that A(Ref)=[14.5534 14.5534 90.4512 90.4512], B= [12.3444 14.1201 14.4404 13.5999 90.419 90.850] How can I get the nearest values of A(Ref) from B? output result should be= [14.4404 14.1201 90.850 90.419] Thanks... D.B

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2015-9-10
Assuming the above comment is true...
>> interp1(B,B,A,'nearest')
ans =
14.4400 14.1200 90.4190 90.8500
  2 个评论
D Bhuiyan
D Bhuiyan 2015-9-10
Thanks for your feedback and I already did it. Here I did a mistake, actually I would like to see: If two vectors are like that A(Ref)=[14.5534 14.5534 90.4512 90.4512], B= [12.3444 14.1201 14.4404 13.5999 90.419 90.850] How can I get the nearest values of A(Ref) from B? output result should be= [14.4404 14.1201 90.850 90.419] Thanks... D.B
Image Analyst
Image Analyst 2015-9-10
Again, for the second element, how is 14.5534 (second element of A) closer to 14.1201 (from B vector and also your output), than is 14.4404 (also from B vector)???

请先登录,再进行评论。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by