meshgrid.h 1.44 KB
``````#ifndef STIM_MESHGRID_H
#define STIM_MESHGRID_H

namespace stim{

/// Create a 2D grid based on a pair of vectors representing the grid spacing (see Matlab)
/// @param X is an [nx x ny] array that will store the X coordinates for each 2D point
/// @param Y is an [nx x ny] array that will store the Y coordinates for each 2D point
/// @param x is an [nx] array that provides the positions of grid points in the x direction
/// @param nx is the number of grid points in the x direction
/// @param y is an [ny] array that provides the positions of grid points in the y direction
/// @param ny is the number of grid points in the y direction
template<typename T>
void meshgrid(T* X, T* Y, T* x, size_t nx, T* y, size_t ny){
size_t xi, yi;												//allocate index variables
for(yi = 0; yi < ny; yi++){									//iterate through each column
for(xi = 0; xi < nx; xi++){								//iterate through each row
X[yi * nx + xi] = x[xi];
Y[yi * nx + xi] = y[yi];
}
}
}

/// Creates an array of n equally spaced values in the range [xmin xmax]
/// @param X is an array of length n that stores the values
/// @param xmin is the start point of the array
/// @param xmax is the end point of the array
/// @param n is the number of points in the array
template<typename T>
void linspace(T* X, T xmin, T xmax, size_t n){
T alpha;
for(size_t i = 0; i < n; i++){
alpha = (T)i / (T)n;
X[i] = (1 - alpha) * xmin + alpha * xmax;
}
}

}

#endif``````