How to get unknown number of variables to be all same length through interpolation
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have N variables, all of different lengths. I have no prior knowledge of which variables are what lengths BUT I need to get all variables interploated to the same length.
I load in a file that contain a time of length M and a data field of length M, then another with time length M+1 and data field length M+1. The first and last time of every file are the same, but the number of data points is not (different sampling rates and/or invalid samples removed). So I cant just add NaNs to the beginning or end to make them all the same.
If it is like 2 variables I can easily just check the lengths, have an if statement for which one is longer and interpolate the longer one to the length of the shorter one (or the shorter to the longer but I prefer to downsample).
I was trying to do some sort of loop over the number of variables or something but I could not figure out an easy solution that didnt involve manually inputting the lengths or something similiar.
0 comentarios
Respuestas (1)
Star Strider
el 14 de En. de 2021
Editada: Star Strider
el 14 de En. de 2021
Try this approach:
V1 = sort(rand(1,5)); % Original Vector
V2 = sort(rand(1,10)); % Original Vector
N = 20; % Required Vector Length
V1i = interp1((1:numel(V1)), V1, linspace(1, numel(V1), N)); % Interpolated Vector
V2i = interp1((1:numel(V2)), V2, linspace(1, numel(V2), N)); % Interpolated Vector
EDIT — (14 Jan 2021 at 14:22)
Plotting them (in one random example):
V1xi = linspace(1, numel(V1), N);
V2xi = linspace(1, numel(V2), N);
figure
hold on
plot((1:numel(V1)), V1, 'o-')
plot((1:numel(V2)), V2, 'o-')
plot(V1xi, V1i, 'x-')
plot(V2xi, V2i, 'x-')
hold off
grid
produces:
.
6 comentarios
Ver también
Categorías
Más información sobre NaNs en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!