I need help to interpolate non-square matrices in my code, because the code I have been working on works only for square matrices.
Thank you
clc; clear; close all;
V = [600.1 645.23 865.17 1425.8 1254.4;
1012.1 1055.8 1006.3 1537.1 1212;
1296.6 1616.6 1351.9 1393.5 1391.9;
639.5 868.24 890.56 1644.9 1770.8;
639.5 868.24 890.56 1644.9 1770.8];
ecol = 30;
ncol = 5 ;
efil = 30;
nfil = 5 ;
%% Matriz de valores para la interpolación del paño
x = (0:ecol:(ncol-1)*ecol);
for i = 1:ncol
X(i,:)= x;
end
y = (0:efil:(nfil-1)*efil)';
for k = 1:nfil
Y(:,k)= y;
end
x = (0:1:(ncol-1)*ecol);
for i = 1:(nfil-1)*efil + 1
Xq(i,:)= x;
end
y = (0:1:(nfil-1)*efil)';
for k = 1:(ncol-1)*ecol + 1
Yq(:,k)= y;
end
M1q = interp2(X,Y,V,Xq,Yq,'cubic');
%% Ploteo del mapa de velocidades
figure('units','normalized','outerposition',[0 0 1 1])
s = contourf(Xq,Yq,M1q,'--','showtext', 'on');
view(2)
colorbar
set(gca,'Ydir','reverse');
axis tight
title("ST1 [m/s]", 'Fontsize', 15, 'Fontname', 'Times New Roman')
xlabel("Horizontal Displacement [cm]", 'Fontsize', 15, 'Fontname', 'Times New Roman')
ylabel("Vertical Displacement [cm]", 'Fontsize', 15, 'Fontname', 'Times New Roman')

 採用された回答

Voss
Voss 2022 年 6 月 8 日
編集済み: Voss 2022 年 6 月 8 日

2 投票

There is an error in how your matrices X and Y are constructed: nfil and ncol are swapped in those two for loops. It's corrected here:
clc; clear; close all;
V = [600.1 645.23 865.17 1425.8 1254.4;
1012.1 1055.8 1006.3 1537.1 1212;
1296.6 1616.6 1351.9 1393.5 1391.9;
...639.5 868.24 890.56 1644.9 1770.8;
639.5 868.24 890.56 1644.9 1770.8];
ecol = 30;
efil = 30;
[nfil,ncol] = size(V); % avoid hard-coding nfil and ncol; get them from size(V) instead
%% Matriz de valores para la interpolación del paño
x = (0:ecol:(ncol-1)*ecol); % x = (0:ncol-1)*ecol; % would be better
for i = 1:nfil % this was ncol
X(i,:)= x;
end
y = (0:efil:(nfil-1)*efil)'; % y = (0:nfil-1).'*efil; % would be better
for k = 1:ncol % this was nfil
Y(:,k)= y;
end
disp(X); disp(Y);
0 30 60 90 120 0 30 60 90 120 0 30 60 90 120 0 30 60 90 120 0 0 0 0 0 30 30 30 30 30 60 60 60 60 60 90 90 90 90 90
Note that you can use the meshgrid function to construct X and Y the same way.
[X,Y] = meshgrid(x,y);
disp(X); disp(Y);
0 30 60 90 120 0 30 60 90 120 0 30 60 90 120 0 30 60 90 120 0 0 0 0 0 30 30 30 30 30 60 60 60 60 60 90 90 90 90 90
x = (0:1:(ncol-1)*ecol);
for i = 1:(nfil-1)*efil + 1 % could use Xq = repmat(x,nfil,1); instead of a loop
Xq(i,:)= x;
end
y = (0:1:(nfil-1)*efil)';
for k = 1:(ncol-1)*ecol + 1 % could use Yq = repmat(y,1,ncol); instead of a loop
Yq(:,k)= y;
end
M1q = interp2(X,Y,V,Xq,Yq,'cubic');
%% Ploteo del mapa de velocidades
figure('units','normalized','outerposition',[0 0 1 1])
s = contourf(Xq,Yq,M1q,'--','showtext', 'on');
view(2)
colorbar
set(gca,'Ydir','reverse');
axis tight
title("ST1 [m/s]", 'Fontsize', 15, 'Fontname', 'Times New Roman')
xlabel("Horizontal Displacement [cm]", 'Fontsize', 15, 'Fontname', 'Times New Roman')
ylabel("Vertical Displacement [cm]", 'Fontsize', 15, 'Fontname', 'Times New Roman')

3 件のコメント

Stephen23
Stephen23 2022 年 6 月 8 日
Xq and Yq should also be replaced by MESHGRID. All of the loops are superfluous and inefficient.
Anghelo Centeno Durand
Anghelo Centeno Durand 2022 年 6 月 9 日
Dear thank you for your help
you saved me a week of doubts
Voss
Voss 2022 年 6 月 9 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

製品

リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by