MATLAB: 3D Surface plot using truncated set of data

3d plotsurface

Dear All,
I have two variables lets say, X and Y and a performance variable, Z = f(X,Y). X and Y, both are vectors of length 10. Now, Z is measured from a physical experiment for all possible combinations of X and Y that is 100 data points. However, there are few combinations of X and Y which are invalid and should not be considered. So, after the end of the experiment, I have the Z matrix of size 10 x 10, but few of the cells are invalid/NULL. Now, how can I plot the rest of the data in form of a 3D surface in MATLAB?
– Sam.

Best Answer

x = linspace(0,1,10) ;
y = linspace(0,1,10) ;
[X,Y] = meshgrid(x,y) ;
Z = randi([1,10],10,10) ;
idx = find(Z==10) ;
Xi = NaN(size(X)) ;
Xi(idx) = X(idx) ;
Yi = NaN(size(Y)) ;
Yi(idx) = Y(idx) ;
Zi = NaN(size(Z)) ;
Zi(idx) = Z(idx) ;
surf(Xi,Yi,Zi)
[Xi,Yi,Zi] = find(Z==10) ;
scatter(Xi,Yi,Zi)