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
回答(2 个)
Star Strider
2017-10-10
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 个评论
Walter Roberson
2017-10-10
Be careful, the unique() is not necessarily justified, as the data might contain duplicate or overlapping ranges, and is not necessarily in sorted order.
Walter Roberson
2017-10-10
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!