I have a grid that I want to store x and y coordinates for in 1D arrays. It is orthogonal so that x values are the same at each y in the grid, and vice versa. I've recently tried to vectorize the procedure and am using a repmat
-reshape
combination to achieve this. I heard that repmat
is not the best, and wondered if there are faster alternatives to this same procedure.
For example, if the grid contains 3 nodes in the x direction (nX = 3
) and 4 in the y direction (nY = 4
), and their "template" values are as follows:
xTemplate = [0 0.5000 1.0000]
yTemplate = [0 0.3333 0.6667 1.0000]
Then the values for the entire grid would look like:
xArray =
0 0.5000 1.0000
0 0.5000 1.0000
0 0.5000 1.0000
0 0.5000 1.0000
and
yArray =
0 0 0
0.3333 0.3333 0.3333
0.6667 0.6667 0.6667
1.0000 1.0000 1.0000
Ultimately, I want them to be contained in 1D arrays, where the values are stored in order from 1
to nX*nY
, in other words where
iVec = nX*(jArray - 1) + iArray
So that they look as follows:
[xVec yVec] =
0 0
0.5000 0
1.0000 0
0 0.3333
0.5000 0.3333
1.0000 0.3333
0 0.6667
0.5000 0.6667
1.0000 0.6667
0 1.0000
0.5000 1.0000
1.0000 1.0000
Currently, I have the following code to achieve this, but am wondering if there are other ways to increase the speed here:
nX = 3;
nY = 4;
xTemplate = linspace(0,1,nX);
xArray = repmat(xTemplate, nY, 1);
xVec = reshape(xArray', [nX*nY 1]);
yTemplate = linspace(0,1,nY);
yArray = repmat(yTemplate, nX, 1)';
yVec = reshape(yArray' , [nX*nY 1]);
What are some alternatives to this that might increase speed? Is there a simpler approach that omits creating xArray
and yArray
which are only intermediate and not necessary? Thanks for any help.