Interpolate matrices for different times in Matlab

7 ビュー (過去 30 日間)
mldmnn
mldmnn 2018 年 8 月 9 日
コメント済み: mldmnn 2018 年 8 月 10 日
I have computed variables stored in a matrix for a specific time vector. Now I want to interpolate between those whole matrices for a new time vector to get the matrices for the desired new time vector.
I've came up with the following solution but it seems clunky and computational demanding:
clear all;
a(:,:,1) = [1 1 1;2 2 2;3 3 3]; % Matrix 1
a(:,:,2) = [4 4 4;6 6 6;8 8 8]; % Matrix 2
t1 = [1 2]; % Old time vector
t2 = [1 1.5 2]; % New time vector
% Interpolation for each matrix element
for r = 1:1:size(a,2)
for c = 1:1:size(a,1)
tab(:) = a(r,c,:);
tabInterp(r,c,:) = interp1(t1,tab(:),t2);
end
end
The result is and should be:
[2.5000 2.5000 2.5000
4.0000 4.0000 4.0000
5.5000 5.5000 5.5000]
Any thoughts?

採用された回答

Stephen23
Stephen23 2018 年 8 月 10 日
編集済み: Stephen23 2018 年 8 月 10 日
Simpler in just one line and more efficient without all of those intermediate variables:
>> a(:,:,1) = [1 1 1;2 2 2;3 3 3];
>> a(:,:,2) = [4 4 4;6 6 6;8 8 8];
>> b = permute(interp1([1,2],permute(a,[3,2,1]),[1,1.5,2]),[3,2,1])
b(:,:,1) =
1 1 1
2 2 2
3 3 3
b(:,:,2) =
2.5000 2.5000 2.5000
4.0000 4.0000 4.0000
5.5000 5.5000 5.5000
b(:,:,3) =
4 4 4
6 6 6
8 8 8
And compared to your original code:
>> isequal(tabInterp,b)
ans = 1
  1 件のコメント
mldmnn
mldmnn 2018 年 8 月 10 日
That is even better than the other answer! Nice to learn from experts!

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2018 年 8 月 9 日
You can use interp3 as follow:
clear all;
a(:,:,1) = [1 1 1;2 2 2;3 3 3]; % Matrix 1
a(:,:,2) = [4 4 4;6 6 6;8 8 8]; % Matrix 2
t1 = [1 2]; % Old time vector
t2 = [1 1.5 2]; % New time vector
% Interpolation for each matrix element
[X, Y, Z] = meshgrid(1:size(a,1), 1:size(a,2), t1);
[X2, Y2, Z2] = meshgrid(1:size(a,1), 1:size(a,2), t2);
tabInterp = interp3(X,Y,Z,a,X2,Y2,Z2);
  2 件のコメント
mldmnn
mldmnn 2018 年 8 月 10 日
It works very well. Thank you! Didn't think about using interp3 for this issue.
Ameer Hamza
Ameer Hamza 2018 年 8 月 10 日
You are welcome.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by