Filter löschen
Filter löschen

find indexes of two values that between them there is specific number

5 Ansichten (letzte 30 Tage)
Keren Grinberg
Keren Grinberg am 28 Apr. 2022
Beantwortet: David Hill am 28 Apr. 2022
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!

Antworten (2)

Davide Masiello
Davide Masiello am 28 Apr. 2022
Bearbeitet: Davide Masiello am 28 Apr. 2022
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))
newA = 1×11
1.0000 3.0000 5.0000 4.0000 3.0000 6.5000 10.0000 24.0000 38.0000 32.5000 27.0000
interpA = newA(2:2:end)
interpA = 1×5
3.0000 4.0000 6.5000 24.0000 32.5000
% Find closest interpolated value
[~,k] = min(abs(interpA-B));
disp(interpA(k))
3
% Indexes of elements in A that enclose the closest interpolated value
A_idxs = [k,k+2]
A_idxs = 1×2
1 3

David Hill
David Hill am 28 Apr. 2022
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)

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by