Matrix Expansion Row Wise Using Interpolation
16 ビュー (過去 30 日間)
古いコメントを表示
Hey,
I have a matrix X of 6000 by 12 elements, I would like to expand the matrix X to be 12000 by 12 by interpolation between two rows. The data is from an experiment and each row is a timed measurement of 12 variables.
I have had a look at interp1 and interp2 but it is not making much sense.
The way I can do this is by using two for loops one that does go through each column the other that goes through each element within a column, compares them and inserts a value in between two existing. This seems rather complicated and I am sure there are methods in Matlab that can do exactly that.
What I ideally want is to be able to take a matrix of measurements, down sample it by removing each second row, then expand it back up using interpolation, compare the results, then repeat by removing more consecutive rows and re interpolating. Look at it as a downsample and upsample process in which I want to see at which point I am beginning to lose valuable information.
for j=1:size(X_downsampled,1)-1
error = X_downsampled(j+1,:) - X_downsampled(j,:);
error = error ./ 2;
X_Upsampled = [X_Upsampled; X_downsampled(j,:)];
X_Upsampled = [X_Upsampled; (X_downsampled(j,:)' + error')'];
if j == size(X_downsampled,1)-1
X_Upsampled = [X_Upsampled; X_downsampled(j+1,:)];
end
end
There is probably a much better and cleverer way of doing this.
Thanks a lot
1 件のコメント
Stephen23
2015 年 6 月 5 日
編集済み: Stephen23
2015 年 6 月 5 日
Concatenating arrays inside a loop is slow and inefficient, as MATLAB has to check and move the array every time it grows beyond the current allocated memory. This has been discussed many times on this forum, and you will find lots of advice online about how to avoid doing this. This is a good start:
回答 (3 件)
Stephen23
2015 年 6 月 5 日
編集済み: Stephen23
2015 年 6 月 5 日
Here is a complete working example of how you could use interp1 to interpolate between the rows of a matrix:
>> A = [1,2,3;3,6,9;5,10,15;7,14,21]
A =
1 2 3
3 6 9
5 10 15
7 14 21
>> Xi = 2*(1:size(A,1));
>> Xo = 2:Xi(end);
>> B = interp1(Xi,A,Xo)
B =
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
6 12 18
7 14 21
>> B(1:2:end,:)
ans =
1 2 3
3 6 9
5 10 15
7 14 21
2 件のコメント
Stephen23
2015 年 6 月 5 日
編集済み: Stephen23
2015 年 6 月 6 日
You can change one value in two places: the 2 on the line that defines Xi, and in Xo. This sets how many lines the output matrix with have for each line of the input matrix (as a simple introduction to using interpolation):
>> N = 4;
>> Xi = N*(1:size(A,1));
>> Xo = N:Xi(end);
>> B = interp1(Xi,A,Xo)
B =
1.0000 2.0000 3.0000
1.5000 3.0000 4.5000
2.0000 4.0000 6.0000
2.5000 5.0000 7.5000
3.0000 6.0000 9.0000
3.5000 7.0000 10.5000
4.0000 8.0000 12.0000
4.5000 9.0000 13.5000
5.0000 10.0000 15.0000
5.5000 11.0000 16.5000
6.0000 12.0000 18.0000
6.5000 13.0000 19.5000
7.0000 14.0000 21.0000
Guillaume
2015 年 6 月 5 日
編集済み: Guillaume
2015 年 6 月 5 日
interp1 is the function to use (unless there's something in the signal processing toolbox).
%demo X consisting of 12 columns of sinusoidals at various frequency
timings = linspace(0, 2*pi, 6000)';
X = cell2mat(arrayfun(@(f) sin(f*timings), 1:12, 'UniformOutput', false));
figure;plot(timings, X);
newtimings = linspace(timings(1), timings(end), numel(timings)*2)'; %twice as many samples
X_upsampled = interp1(timings, X, newtimings);
figure;plot(newtimings, X_upsampled);
2 件のコメント
Guillaume
2015 年 6 月 8 日
I'm not sure what you mean by missing values, but to insert a number of points in between each original timings, with guarantee that all your original points are still present, you could use:
originaltimings = [0:4, 5:2:9, 11:3:20]'; %demo data
numpointstoinsert = 3;
increments = linspace(0, 1, numpointstoinsert + 1);
scaledincrements = bsxfun(@times, diff(originaltimings), increments(1:end-1));
newtimings = bsxfun(@plus, originaltimings(1:end-1), scaledincrements)';
newtimings = [newtimings(:); originaltimings(end)]
Antonio Jossimar Lazos Toledo
2018 年 4 月 11 日
this help me a lot, but how i can use this to interpolate columns? can anyone help me
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!