How to get unknown number of variables to be all same length through interpolation
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Star Strider
2021년 1월 14일
편집: Star Strider
2021년 1월 14일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!