# Help using @nloops and @nref

**URL:** <https://discourse.julialang.org/t/help-using-nloops-and-nref/50397>\
**Category:** General Usage\
**Created:** [November 18, 2020, 8:01pm UTC](https://discourse.julialang.org/t/help-using-nloops-and-nref/50397 "2020-11-18T20:01:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [November 18, 2020, 8:01pm UTC](https://discourse.julialang.org/t/help-using-nloops-and-nref/50397/1 "2020-11-18T20:01:37Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 18, 2020, 9:22pm UTC](https://discourse.julialang.org/t/help-using-nloops-and-nref/50397/2 "2020-11-18T21:22:41Z")

</div>

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](https://julialang.org/blog/2016/02/iteration/).

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

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [November 18, 2020, 9:39pm UTC](https://discourse.julialang.org/t/help-using-nloops-and-nref/50397/3 "2020-11-18T21:39:55Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![gregory](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregory/32/18969_2.png) [@gregory](https://discourse.julialang.org/u/gregory)\
**Post date:** [November 18, 2020, 11:06pm UTC](https://discourse.julialang.org/t/help-using-nloops-and-nref/50397/4 "2020-11-18T23:06:48Z")

</div>

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.
