Which is a trick I sometimes use, but this is harder to read if you're unfamiliar with the pattern. You could also replace the calls to ravel with a, b = np.mgrid.reshape(2, -1) The way this works is that x has shape (100, 1) after we inject a trailing singleton (in MATLAB trailing singletons are implied, in numpy leading ones), and both a.ravel() and b.ravel() have shape (10*10,) which is compatible with (1, 10*10), making broadcasting possible into shape (100, 10*10). ravelling (or reshaping) your a and b arrays: x = np.linspace(-1, 1, 100) # inject trailing singleton for broadcasting You can do away with the loop with broadcasting and getting your desired 2d array by. If you do that with numpy you get slices of an array instead, just as I mentioned with D. Secondly, in MATLAB using a linear index into a 2d array (namely a and b) will give you flattened views. Y should be the same size as Z or a vector of size m. If size (Z) m, n, then X should be of the same size as Z or a vector of size n. Matrices X, Y are plotted along the x-y plane and plots matrix Z as height above the x-y plane. The latter is equivalent to D which is in your case D. Mesh (X, Y, Z) It plots X, Y, Z on a three-dimensional surface.
#MATLAB 2008 MESHGRID HOW TO#
The rows of the matrix correspond to different quadratic coefficients, while each column corresponds to a grid point at which the function is evaluated.īut I always get broadcasting errors which I am not sure how to fix.įirstly, in MATLAB you're assigning to D(:,i), but in python you're assigning to D. I then make a uniform meshgrid of a and b. I am evaluating the function ax^2 +bx + 1 on a grid. I am attempting to write a program which constructs a matrix and performs a singular value decomposition on it.