Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of the form {{1,1,1}→1.2, {1,1,2}→20.2, ..., {m,n,p}→0.3} and I want to create an $m \times n \times p$ array, $A$, where the value of $A[[i,j,k]]$ is as specified by the list (e.g., $A[[1,1,2]] == 20.2$ in the example above). This array represents a series of $p$ matrices, each $m \times n$, and each of which represents two-dimensional imaging data.

The array $A$ can be formed by Normal[SparseArray[{{1,1,1}→1.2, {1,1,2}→20.2, ..., {m,n,p}→0.3}]]. However, I'm looking for the fastest way to create the array $A$. Specifically, I'm looking for a method that minimizes runtime (see below). Is the above approach the best, or is there a faster one? Typical values for $m, n, p$ are 320, 320, 20, respectively. The values to be stored are machine-precision numbers. The array is not particularly sparse (perhaps as much as 20% of the elements will be zero).

I need to create many such $A$ arrays within an optimization (each instance of $A$ will differ from the previous one as the optimization proceeds). Once the array has been created, further processing will be applied (the first thing that will be done is to apply a two-dimensional discrete Fourier transform to each of the $p$ matrices. I assume for this reason that it makes sense for the array $A$ to be dense rather than sparse (or, at least) for each of the $p$ matrices to be dense.

share|improve this question
Good question: likely between none and about 10%-20% of them. – Chris Mar 12 '14 at 21:50
Also, what do you mean by "fastest"? Is what you're doing in any way slow? Or are you asking about a convenient way to generate that list of rules? – R. M. Mar 12 '14 at 21:52
By fastest I mean "takes the least time to run". What I'm doing is not slow per se, but I need to do it a lot (many thousands of times), so even modest time savings will be useful. – Chris Mar 12 '14 at 22:27
I guess my point was that we don't know what you know about your values or rules or how you have your data. Where do those numbers come from? Is it stored in a list? What dimension list? Or do you have those positions and rules already generated? It is not clear which part of the problem needs speeding up. – R. M. Mar 12 '14 at 22:33
Why do you need the matrix to be dense and not sparse? What is the density of your matrix (ratio of non-zero elements to total number of elements)? Are you sure that the matrix creation is the bottleneck in your code, and not something else? What are you doing with the matrix after you create it? – Szabolcs Mar 12 '14 at 22:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.