How to add data between numbers in a vector.
Mostrar comentarios más antiguos
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
Respuestas (2)
Star Strider
el 10 de Oct. de 2017
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 comentario
Walter Roberson
el 10 de Oct. de 2017
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
el 10 de Oct. de 2017
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.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!