Last update 2.2.99
L3.html

More on meshgrid

Let's make a "Maple-emulation" of the transparency shown: (Text-mode Maple is often quite reasonable for making "poor man's mathematical notation" on the web.)
> with(linalg): 

Here's the matrix we would actually like to build in Matlab numerically 
(actually, with cell arrays (version 5) it is possible but perhaps all 
functions don't support it and it may be less efficient.)

> XY:=matrix(3,4,(i,j)->[x[j],y[i]]);
              [[x[1], y[1]]    [x[2], y[1]]    [x[3], y[1]]    [x[4], y[1]]]
              [                                                            ]
        XY := [[x[1], y[2]]    [x[2], y[2]]    [x[3], y[2]]    [x[4], y[2]]]
              [                                                            ]
              [[x[1], y[3]]    [x[2], y[3]]    [x[3], y[3]]    [x[4], y[3]]]

The "height matrix" is now obtained simply by "mapping" the given function
f on each part of the structure (each element of the matrix).

> Z1:=map(f,XY); 
      [f([x[1], y[1]])    f([x[2], y[1]])    f([x[3], y[1]])    f([x[4], y[1]])]
      [                                                                        ]
Z1 := [f([x[1], y[2]])    f([x[2], y[2]])    f([x[3], y[2]])    f([x[4], y[2]])]
      [                                                                        ]
      [f([x[1], y[3]])    f([x[2], y[3]])    f([x[3], y[3]])    f([x[4], y[3]])]


Now, let's do things the Matlab meshgrid-way. There is an elegant general way 
of building matrices. Just give the size of the matrix and a function of the 
indices to define the elements.


> X:=matrix(3,4,(i,j)->x[j]);
                           [x[1]    x[2]    x[3]    x[4]]
                           [                            ]
                      X := [x[1]    x[2]    x[3]    x[4]]
                           [                            ]
                           [x[1]    x[2]    x[3]    x[4]]

> Y:=matrix(3,4,(i,j)->y[i]);
                           [y[1]    y[1]    y[1]    y[1]]
                           [                            ]
                      Y := [y[2]    y[2]    y[2]    y[2]]
                           [                            ]
                           [y[3]    y[3]    y[3]    y[3]]

> ?zip zip - zip together two lists or vectors Calling Sequence: zip(f, u, v) or zip(f, u, v, d) Parameters: f - binary function u,v - lists or vectors d - value (optional) Description: - The function zip applies the binary function f to the components of two lists/vectors u and v creating a new list/vector r defined as follows. If m is the length of u and n is the length of v then r is a list/vector of length min(m,n) with r[i] = f(u[i],v[i]) for i in 1..min(m,n).
> zip(f,X,Y); Error, (in zip) can only zip lists and vectors Well, unfortunately there's this restriction. So we must go around it by converting to lists and then back. > XL:=map(op,convert(X,listlist)); XL := [x[1], x[2], x[3], x[4], x[1], x[2], x[3], x[4], x[1], x[2], x[3], x[4]] > YL:=map(op,convert(Y,listlist)); YL := [y[1], y[1], y[1], y[1], y[2], y[2], y[2], y[2], y[3], y[3], y[3], y[3]] > zip(f,XL,YL); [f(x[1], y[1]), f(x[2], y[1]), f(x[3], y[1]), f(x[4], y[1]), f(x[1], y[2]), f(x[2], y[2]), f(x[3], y[2]), f(x[4], y[2]), f(x[1], y[3]), f(x[2], y[3]), f(x[3], y[3]), f(x[4], y[3])] > Z2:=matrix(3,4,zip(f,XL,YL)); [f(x[1], y[1]) f(x[2], y[1]) f(x[3], y[1]) f(x[4], y[1])] [ ] Z2:= [f(x[1], y[2]) f(x[2], y[2]) f(x[3], y[2]) f(x[4], y[2])] [ ] [f(x[1], y[3]) f(x[2], y[3]) f(x[3], y[3]) f(x[4], y[3])]
So we have a Maple-emulation for Matlab's meshgrid-way of building surface plot data. This led me to the idea for Solution to direction field of an autonomous 2 x 2 system.