how to increase the number of samples in a vector using interpolation

8 vues (au cours des 30 derniers jours)
stud
stud le 24 Fév 2018
Modifié(e) : Stephen23 le 24 Fév 2018
i have n number of vectors each having different number of samples. It is required to have the same number of samples in each vector. how can i do this using interpolation. for example vec1= [3 5 2 1]; vec2=[4 5 3 4 3 1 4] ; i required that vec1 and vec2 should be of same length say 10 please help
  1 commentaire
Stephen23
Stephen23 le 24 Fév 2018
"i have n number of vectors..."
Note that accessing variable names in a loop is slow, complex, buggy, and hard to debug:
Putting these vector into one cell array would mean you could trivially loop over them all using a for loop and indexing, which is simple, intuitive, and very efficient.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 24 Fév 2018
Modifié(e) : Stephen23 le 24 Fév 2018
>> vec1 = [3 5 2 1];
>> vec2 = [4 5 3 4 3 1 4];
>> xo1 = 1:numel(vec1);
>> xo2 = 1:numel(vec2);
>> xi1 = linspace(1,numel(vec1),10);
>> xi2 = linspace(1,numel(vec2),10);
>> interp1(xo1,vec1,xi1)
ans =
3 3.6667 4.3333 5 4 3 2 1.6667 1.3333 1
>> interp1(xo2,vec2,xi2)
ans =
4 4.6667 4.3333 3 3.6667 3.6667 3 1.6667 2 4
"i have n number of vectors ..."
When you put them into one cell array then you can trivially loop over all of them:
C = {[3 5 2 1], [4 5 3 4 3 1 4], ...};
D = cell(size(C));
for k = 1:numel(C)
N = numel(C{k});
xo = 1:N;
xi = linspace(1,N,10);
D{k} = interp1(xo,C{k},xi);
end
  2 commentaires
WEEKIAN SOH
WEEKIAN SOH le 24 Fév 2018
Dear Stephen Cobeldick, my name is Kian, and I am a fan of your function, num2words(). Absolutely amazing! You had just managed to make our MATLAB software output readable text!!! I'll be following your post closely, and I look forward to learning from you the Master of MATLAB.
Stephen23
Stephen23 le 24 Fév 2018
Modifié(e) : Stephen23 le 24 Fév 2018
@WEEKIAN SOH: I am glad you like num2words. If you have the time please add a comment and rating at the bottom of the FEX page, so that you can tell others that you found it useful.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by