find indexes of two values that between them there is specific number
8 次查看(过去 30 天)
显示 更早的评论
i have vector A=[1 5 3 1 10 38 27] and value B=2 i want to find the nearest value to B by interpolate values that B is between them. like this: 2 is between 1,5 and 3,1 if 2 is closest to value from linear interpolation between 1,5 i want to chose this number if 2 is closest to value from linear interpolation between 3,1 i want to chose this number
thanks!
回答(2 个)
Davide Masiello
2022-4-28
编辑:Davide Masiello
2022-4-28
clear,clc
A = [1 5 3 10 38 27];
B = 2;
% Interpolattion values
idx = 1:2:2*length(A)-1;
newA = interp1(idx,A,1:idx(end))
interpA = newA(2:2:end)
% Find closest interpolated value
[~,k] = min(abs(interpA-B));
disp(interpA(k))
% Indexes of elements in A that enclose the closest interpolated value
A_idxs = [k,k+2]
0 个评论
David Hill
2022-4-28
A=[1 5 2.5 1 2.1 38 1.4 2.7 27 2.3 1.9 2.6 2.7 9 10 1.2];
B=2;
f=A-B;
F=find(f<0);
b=[];
for k=1:length(F)
try
[~,i]=min([abs(f(F(k))),abs(f(F(k)-1))]);
b=[b,A(F(k)-i+1)];
end
try
[~,i]=min([abs(f(F(k)+1)),abs(f(F(k)))]);
b=[b,A(F(k)-i+2)];
end
end
%b array will be A-values closest to B (when adjacent values of A are
%between B)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!