0

I found this solution concatenate empty array, but I don't believe it fully addresses my issue. I want a more general approach that avoids adding an if statement to every instance of concatenation. Several functions within a MATLAB script that I'm transcribing into Python come after an if statement that initializes FixedDictionaryElement, which is a 2-D array.

if (param.preserveDCAtom>0)
    FixedDictionaryElement(1:size(Data,1),1) = 1/sqrt(size(Data,1));
else
    FixedDictionaryElement = [];

If this condition is met, a 2-d array is initialized, filled with data, and later concatenated with another 2-D array in several different places; however, if the condition isn't met, an empty array is initialized FixedDictionaryElement = [], but it's still concatenated in the same places like the example I've given below. I assume MATLAB is simply concatenating the empty array, which ends up being like multiplying a number by 1. The filled array is unaffected by the empty array, and the program continues unabated. FixedDictionaryElement is the empty array in this case.


if (param.errorFlag==0)
        CoefMatrix = OMP(**[FixedDictionaryElement,Dictionary]**,Data, param.L);

Assume FixedDictionaryElement = [] and Dictionary = 34x80.

From looking at the MATLAB code, I assume the empty array is initialized to allow for the concatenation to be done throughout the script, irrespective of the result of the if statement. Otherwise, you'd get an error that FixedDictionaryElement is undefined without the empty array.

How can I generalize the solution given in the above link without putting a new if statement at every instance of concatenation?

spbenit
  • 31
  • 4
  • 1
    Regardless of what MATLAB is doing, in `numpy` it is a bad idea to repeatedly `concatenate` arrays, since that involves a lot of new arrays and copying. Collect your lists/arrays in a list (or list of lists), and do the array creation once. List append just adds references to the list, and is quite efficient. It's also easier to ensure that all arrays have the correct matching dimensions (none of this silly "empty" array business). This topic comes nearly every day under the [numpy] tag. – hpaulj Jun 16 '20 at 23:31
  • 1
    On the topic of matching shapes, the basic array tool is `np.concatenate`. It is picky about the number of dimensions, and matching dimensions (except the join axis). The various `stack` functions use it, but tweak the number of dimensions in one way or other. Look at their code for examples of how to adjust dimensions - it's short and readable. – hpaulj Jun 16 '20 at 23:38
  • @hpaulj thank you for the response. Is there a Pythonic standard procedure for creating arrays you'll need throughout your module at the beginning of the module? If I'm reading you correctly, you recommend preemptively initializing arrays in an array of arrays (this could also be a dictionary?), and appending dimensions or values as the arrays are updated throughout the module? I can give you some more context from my code if you feel like we're talking past each other. – spbenit Jun 17 '20 at 16:22
  • No. I said, or at least implied, that a common practice is to start with a `alist=[]`, and do repeated `alist.append(your_array)`, with a final `np.array(alist)`. But the appended arrays need to be consistent in shape. To make, for example a (N,m,k) array, you need `N` (m,k) shaped arrays. There's no way around the need for consistency. Starting with a `arr = np.zeros((N,m,k)` with repeated `arr[i,:,:]=your_array` also works fine. – hpaulj Jun 17 '20 at 16:36

0 Answers0