How to add data between numbers in a vector.

I am trying to take a given set of data (say [1,2,3,4,5]) and insert values between them using the loop below. When I try this k just becomes 1 and I get an error. How would I insert data into this vector?
for k = 1:length(data)-1
NewData(k) = linspace(data(k),data(k+1),10)
end

Answers (2)

This seems to work:
data = 1:5;
for k = 1:length(data)-1
NewData(:,k) = linspace(data(k),data(k+1),10);
end
NewData = unique(NewData(:))';

1 Comment

Be careful, the unique() is not necessarily justified, as the data might contain duplicate or overlapping ranges, and is not necessarily in sorted order.

Sign in to comment.

skiplast = @(V) V(1:end-1);
temp = arrayfun(@(IDX) skiplast(linspace(data(IDX),data(IDX+1),10)), 1:length(data)-1, 'Uniform', 0);
NewData = [cell2mat(temp), data(end)];
However, I suspect you might find the results a little more natural if you change the 10 to 11.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 10 Oct 2017

Answered:

on 10 Oct 2017

Community Treasure Hunt

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

Start Hunting!