MATLAB: Using matrices to create 3D rendering

error messagemathmathematicsMATLAB

Hello,
I am currently working on a project for my Multivariable calculus class, where we have to turn a set of data into a 3D model.
I currently have mapped out the US and put in the data points for each state, then turned that into a matrix. However, once the matrix is in my code I get the error messages
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.
Error in interp2>makegriddedinterp (line 228)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 128)
F = makegriddedinterp({X, Y}, V, method,extrap);
Error in practice (line 36)
zc = interp2(x,y,z,xi,yi,'cubic');
This is the code I currently have
x=0:50;
y=0:50;
z = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 24609 24609 24609 24609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 24609 24609 24609 24609 24609 27130 31065 31065 31065 31065 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31925 31295 0 0
;0 0 0 24609 24609 24609 24609 24609 27130 31065 31065 31065 31065 31065 31065 31065 0 0 0 0 0 31915 31915 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 31295 31295 31295 0
;0 0 0 27321 27321 27321 27321 27321 27321 31065 31065 31065 31065 31065 31065 31065 0 0 0 0 0 31915 31915 31915 31915 31915 0 0 0 0 0 0 0 0 0 0 0 0 0 31295 31295 31295 0
;0 0 27321 27321 27321 27321 27321 27130 27130 31065 31065 31065 31065 31065 31065 31065 0 0 0 0 0 31915 31915 31915 31915 0 0 0 0 0 0 0 0 0 0 0 0 0 28662 36367 31295 0 0
;0 0 27321 27321 27321 27321 27321 27130 27130 27130 31065 31065 31065 31065 31065 31065 31362 31362 31362 31362 31362 31915 31915 31915 30059 30059 30059 0 0 27532 27532 0 0 0 0 0 30346 30346 28662 36367 0 0 0
;0 0 22744 27321 27321 27321 27321 27130 27130 27130 27130 25378 25378 25378 25378 25378 31362 31362 31362 31362 31362 31915 31915 31915 30059 30059 30059 30059 0 27532 27532 0 0 0 30346 30346 30346 30346 31563 31563 0 0 0
;0 22744 22744 22744 24128 24128 24128 27130 27130 27130 27130 25378 25378 25378 25378 25378 31362 31362 31362 31362 31362 31915 31915 31915 31915 30059 30059 30059 0 27532 27532 27532 0 30346 30346 30346 30346 30346 31217 35494 0 0 0
;0 22744 22744 22744 24128 24128 24128 24128 19975 19975 19975 25378 25378 25378 25378 25378 26585 26585 26585 26585 26585 29801 29801 29801 29801 29801 30059 30059 0 27532 27532 27532 30351 35759 35759 35759 35759 29878 0 0 0 0 0
;0 22744 22744 22744 24128 24128 24128 24128 19975 19975 19975 25378 25378 25378 25378 25378 26585 26585 26585 26585 26585 26585 29801 29801 29801 29801 29271 29271 29562 29562 30351 30351 30351 35759 35759 35759 35759 29878 0 0 0 0 0
;0 0 22744 22744 24128 24128 24128 24128 19975 19975 19975 19975 26520 26520 26520 26520 26520 26585 26585 26585 26585 26585 29801 29801 29801 29271 29271 29271 29562 29562 30351 30351 30351 27708 27455 27455 27455 33838 0 0 0 0 0
;0 0 22744 22744 22744 24128 24128 24128 19975 19975 19975 19975 26520 26520 26520 26520 26520 28776 28776 28776 28776 28776 28776 27532 27532 27532 29271 29271 29562 29562 30351 30351 27708 27708 27708 29296 27455 0 0 0 0 0 0
;0 0 22744 22744 22744 22744 24128 24128 19975 19975 19975 19975 26520 26520 26520 26520 26520 28776 28776 28776 28776 28776 28776 27532 27532 27532 29271 29271 29562 29562 28910 28910 27708 27708 29296 29296 29296 29296 0 0 0 0 0
;0 0 0 22744 22744 22744 24128 23447 23447 23447 23447 21373 21373 21373 21373 21373 21373 28776 28776 28776 28776 28776 28776 27532 27532 27532 27532 29271 28910 28910 28910 28910 28910 25562 25562 25562 25562 25562 0 0 0 0 0
;0 0 0 0 22744 22744 22744 23447 23447 23447 23447 21373 21373 21373 21373 21373 25856 25856 25856 25856 25856 25856 25856 27532 27532 27532 27532 26981 26981 26981 26981 26981 26981 25562 25562 25562 25562 25562 0 0 0 0 0
;0 0 0 0 22744 22744 22744 23447 23447 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 25856 25856 25856 25856 26859 26859 26859 26859 26981 26981 26981 26981 27657 30123 30123 30123 30123 25562 0 0 0 0 0 0
;0 0 0 0 22744 22744 23447 23447 23447 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 25856 25856 25856 25856 26859 26859 26859 29384 29384 31275 31275 31275 27657 27657 30123 30123 30123 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 23447 23447 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 26292 26292 26292 26292 26292 26859 26859 29384 29384 31275 31275 31275 27657 27657 27657 30123 30123 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 0 0 23447 23447 21373 21373 21373 21373 21373 26292 26292 26292 26292 26292 26292 26292 26292 27138 27138 29384 29384 31275 31275 31275 27657 27657 27657 27657 0 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 0 0 0 0 0 0 26292 26292 26292 26292 26292 26292 26292 26292 26292 26292 26292 27138 27138 29384 29384 31275 31275 31275 27657 27657 27657 24461 0 0 0 0 0 0 0 0
;0 0 0 0 26008 26008 26008 26008 0 0 0 0 0 0 26292 26292 26292 26292 26292 26292 26292 26292 26292 26292 27138 27138 27138 27138 31275 24461 24461 24461 24461 24461 24461 0 0 0 0 0 0 0 0
;0 0 0 26008 26008 26008 26008 26008 26008 0 0 0 0 0 0 0 0 26292 26292 26292 26292 26292 26292 26292 27138 27138 27138 27138 0 0 0 0 0 24461 24461 24461 0 0 0 0 0 0 0
;0 0 0 0 26008 26008 26008 26008 26008 0 0 26092 0 26092 0 0 0 0 26292 26292 26292 26292 0 0 0 0 0 0 0 0 0 0 0 0 24461 24461 0 0 0 0 0 0 0
;0 0 0 26008 26008 26008 26008 26008 26008 0 0 0 0 0 26092 26092 0 0 0 26292 26292 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24461 24461 0 0 0 0 0 0
;0 0 0 0 26008 26008 26008 26008 26008 0 0 0 0 0 26902 26092 0 0 0 26292 26292 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24461 24461 0 0 0 0 0 0
;0 0 0 0 26008 0 0 0 0 26008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 0 26008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
;0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
];
xi=0:0.5:50;
yi=0:0.5:50;
[xi,yi] = meshgrid(xi,yi);
zc = interp2(x,y,z,xi,yi,'cubic');
mesh(xi,yi,zc)
I have just started learning MATLAB this year, so all the help I can get would be appreciated.
Thank you

Best Answer

Your z is 29 x 43. When you call interp2(x, y, z, ...) then length(x) must match size(z,2) and length(y) must match size(z,1) so length(x) must be 43 and length(y) must be 29. But you define both x and y as 0:50 which is length(51).
If you have 29 y values that are intended to span from 0 to 50, then use linspace(0, 50, 29) to create the coordinates.