non square matrix interpolation matlab (colorgraph)

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')

 Accepted Answer

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 Comments

Xq and Yq should also be replaced by MESHGRID. All of the loops are superfluous and inefficient.
Dear thank you for your help
you saved me a week of doubts

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!