Help using @nloops and @nref

Hello,

I am trying to build general function that will take the min and max of a K-dimensional array of N data points and use this to create a mesh of a user-specified discretization. For example, here is a 5D array of which I have already extracted the min and max of along each dimension

minX = [ -1.0  -0.5  -1.1  -1.0  -0.9]
maxX = [3.8  2.5  1.8  3.1  2.3]
test=[minX[i]:1.0:maxX[i] for i in 1:5]

This generates the mesh, ideally it would be finer, but for the example, this is fine. I could now just make five for loops and iterate over each ones range, but this seems impractical, especially if the number of dimensions (now 5) increases. The documentation led me to @nloops and @nref, but I cannot figure out the proper syntax needed to have it iterate over the test variable to return what I am expecting which is a matrix that is M x 5 where M = prod([length(x) for x in test]), which is 1200 in this example.

Those macros are the old way of doing things. In general, it’s much easier to simply use iteration over CartesianIndices (or just eachindex of the arrays themselves). A good description of how this works is in this blog post by Tim Holy.

That said, do you really have five dimensional arrays? Or are they just matrices with five columns?

1 Like

I will check it out, thanks! It is based on some data that I would rather be of the form M x d where d is the dimension of the data (in the OP it is d = 5) and M will be dependent on the discretization of each dimension.

So basically, I would like to make a function that takes an the matrix of data points N x d, determine the max and min which I can do. Then specify the discretization (or step size) for each dimension and have it return the matrix M x d.

After reading the blog post it does appear that I misspoke and that what I really want to make is what I described in the second post:

a function that takes in the matrix of data points N x d , determine the max and min which I can do. Then specify the discretization (or step size) for each dimension and have it return the matrix M x d where M = prod([length(x) for x in test]) and have it be flexible to handle the general d dimensional problem.