how to increase the number of samples in a vector using interpolation
8 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 24 Feb 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.
Accepted Answer
Stephen23
on 24 Feb 2018
Edited: Stephen23
on 24 Feb 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 Comments
WEEKIAN SOH
on 24 Feb 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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!