Using griddata to interpolate data of different matrix dimension for a 4D surface plot on MATLAB

49 views Asked by At

I have 4 variables that I want to plot together in a 3D surface plot with color as the 4th dimension. Concentration (x) is a 41x1 matrix, viability (y) is 41x1, frequency (c) is 1x100, and measured data (z) is 41x100.

Each row represents one sample, so there are 41 samples. Each column in frequency and measured data represents the data for all samples at a certain frequency (100 total frequencies). Some points in "measured data" are NaN where they could not be collected.

In order to generate a surface plot, I need to connect the points by interpolating. Using a previously posted solution on Mathworths, I run into issues at the griddata line.

My question: How should griddata be used to generate a surface?

Provided below is an (updated) example with randomly generated data with a similar organization as my true values. A warning message is produced:

Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.  
x= randi(100,1,41); %concentration
x= transpose (x);
x_rep = repmat(x,1,100); %Matrix dimensions 41x100

y= randi(100,1,41); %viability
y= transpose (y);
y_rep = repmat(y,1,100); %Matrix dimensions 41x100

z= randi(100, 41,100); %measured data with dimensions 41x100

c= linspace(1,100,100); %frequency
c = repmat (c, 41, 1); %Matrix dimensions 41x100

%Interpolate
x_interpolate = linspace(min(x), max(x), 100);
y_interpolate = linspace(min(y), max(y), 41);
[X_ex,Y_ex] = meshgrid(x_interpolate,y_interpolate);
z_interpolate = griddata(x_rep(:),y_rep(:),z(:),X_ex,Y_ex); %Problem is here!
c_interpolate= griddata(x_rep,y_rep,c,X_ex,Y_ex); 

%Graph Surface plot
figure
surface(X,Y,z_interpolate,c_interpolate)
grid on
colormap(turbo)                               
colorbar

This fails to produce a surface plot, so my output is below: Failed Surface Plot

0

There are 0 answers