%% Grid Representation
%% Section 1
% This example shows how to create a 2-D grid using meshgrid and ndgrid.
% In MATLAB®, gridded data means data ordered in a grid. You can understand ordered data by thinking about how MATLAB stores data in matrices.
% Define some data.

A = gallery('uniformdata',[3 5],0)

% MATLAB stores the data in a matrix. You can think of A as a set of places for the elements that are ordered by the indices of the matrix. The linear indices of A are:
% \left[ {\begin{array}{*{20}{c}}
% 1&4&7&{10}&{13}\\
% 2&5&8&{11}&{14}\\
% 3&6&9&{12}&{15}
% \end{array}} \right]
% Any element in the matrix can be retrieved by indexing, that is, by asking for the element at that place in the matrix. The ith element in A is retrieved by A(i).
%
% Retrieve the 7th element in A.

A(7)

% For an m-by-n matrix, you can find the column elements adjacent to the ith element by offsetting i by 1. To find the row elements adjacent to the ith element, offset i by m.
% \begin{array}{*{20}{c}}
% {}&{i - 1}&{}\\
% {i - m}&i&{i + m}\\
% {}&{i + 1}&{}
% \end{array}
%
% Retrieve the column elements adjacent to A(7).

A(6),A(8)

% MATLAB uses a similar idea for creating data grids. A grid is not just a set of points that meet certain geometric properties. Rather, a gridded data set relies on an ordered relationship among the points in the grid. The adjacency information readily available in the grid structure is very useful for many applications and particularly grid-based interpolation.
% MATLAB provides two functions for creating grids:
% meshgrid creates 2-D and 3-D grids that are Cartesian axis aligned. To create a 2-D grid, the syntax is [X,Y] = meshgrid(xgv, ygv) where xgv is a vector of length m, and ygv is a vector of length n. meshgrid replicates xgv to form the n-by-m matrix X, and it replicates ygv to form another n-by-m matrix Y. X and Y represent the coordinates of the grid points. The rows of X are aligned with the horizontal X-axis, and the columns of Y are aligned with the negative Y-axis.
% ndgrid creates N-D grids that are array space aligned. In array space the axes are row, column, page, etc. The calling syntax is [X1, X2, X3,...,Xn] = ndgrid(x1gv, x2gv, x3gv,...,xngv) where x1gv,x2gv,x3gv,...,xngv are vectors that span the grid in each dimension. X1,X2,X3,...,Xn are output arrays that can be used for evaluating functions of multiple variables and for multidimensional interpolation.
%
% Use meshgrid to create a 2-D axis aligned grid from two vectors, xgv and ygv.

xgv = [1 2 3];
ygv = [1 2 3 4 5];
[X,Y] = meshgrid(xgv, ygv)

%
% Now use ndgrid to create a 2-D space aligned grid from the same two vectors, xgv and ygv.

[X1,X2] = ndgrid(xgv,ygv)

% Notice that ndgrid's X1 is the transpose of meshgrid's X. The same is true for X2 and Y.
%
% For a given set of inputs, the meshgrid and ndgrid functions will produce grids with exactly the same coordinates. The only difference between their outputs is the format of the coordinate arrays. Plot both outputs and see that they are the same.

figure()
[X1_ndgrid,X2_ndgrid] = ndgrid(1:3,1:5);
Z = zeros(3,5);
mesh(X1_ndgrid,X2_ndgrid,Z,'EdgeColor','black')
axis equal;

% Set the axis labeling and title
h1 = gca;
h1.XTick = [1 2 3];
h1.YTick = [1 2 3 4 5];
xlabel('ndgrid Output')

%

figure()
[X_meshgrid,Y_meshgrid] = meshgrid(1:3, 1:5);
mesh(X_meshgrid,Y_meshgrid,Z','EdgeColor','black')
axis equal;

% Set the axis labeling and title
h2 = gca;
h2.XTick = [1 2 3];
h2.YTick = [1 2 3 4 5];
xlabel('meshgrid Output')

% Depending on how you intend to use your grid, you may prefer one format or the other. Some functions in MATLAB might require your data to have a meshgrid format while others might require an ndgrid format.
% Copyright 2015 The MathWorks, Inc.