x=[1 2 3 4]y=[2 4 5 10]x1=[1,1.5,2.2,3.5,4] how to get previous matching value(from y) for x1 by refering x expected answer y1=[2 2 4 5 10] without forloop
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
deepika Sridhar
il 23 Dic 2021
Commentato: Torsten
il 23 Dic 2021
for i=1:numel(x1)
Idxfound = find (abs(x-x1(i)) < 0.00001); % abs(x-x1(i)) < 0.1
if isempty(Idxfound)
Idxfound=find(x < x1(i));
if isempty(Idxfound)
if i==1
y1(i) = y(1);
else
y1(i)=y1(i-1);
end
else
Idxfound=Idxfound(end);
y1(i) = y(Idxfound);
end
else
y1(i) = y(Idxfound);
end
end
end
0 Commenti
Risposta accettata
Stephen23
il 23 Dic 2021
x = [1,2,3,4];
y = [2,4,5,10];
x1 = [1,1.5,2.2,3.5,4];
%y1=[2,2,4,5,10];
y1 = interp1(x,y,x1,'previous')
4 Commenti
Più risposte (1)
KSSV
il 23 Dic 2021
x=[1 2 3 4];
y=[2 4 5 10];
x1=[1,1.5,2.2,3.5,4] ;
% y1=[2 2 4 5 5] ;
y1 = interp1(x,y,x1)
5 Commenti
KSSV
il 23 Dic 2021
x=[1 2 3 4];
y=[2 4 5 10];
x1=[1,1.5,2.2,3.5,4] ;
% y1=[2 2 4 5 5] ;
n = length(x1);
idx = zeros(n,1) ;
for i = 1:n
[val,id] = sort(abs(x-x1(i))) ;
idx(i) = id(1) ;
end
y1 = y(idx)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!