MATLAB: Construct meshgrid matrices without using function meshgrid.

meshgrid matrices

Hi, I am going to construct meshgrid matrices1 x(i, j), y(i, j), given the grid points x=[0: 0.1:10], y=[0:0.2:10]. when I try x.*y.'. It can't go through. I know I can produce an mn matrix C with elements C(i, j) = x(i)y(j), i=1:m, j= 1:n. But I dunno how can I start with.

Best Answer

Try this:
% Sample data.
x = [0 : 1 : 10] % Change the middle "step" number if you want.
y = [0 : 2 : 10]
% Traditional way using meshgrid().
[X, Y] = meshgrid(x, y)
% Alternate way using loop.
rows = length(y);
columns = length(x);
xLoop = zeros(rows, columns);
yLoop = zeros(rows, columns);
for column = 1 : length(x)
xLoop(:, column) = x(column);
yLoop(:, column) = y;
end
% Print to command window.
xLoop
yLoop