Suppose that f is a function of a single parameter whose output is n-dimensional (m 1 x m 2 x hell m n ) and that B is a vector of length k whose elements are valid arguments for f .
I am looking for a convenient and, more importantly, "agnostic form" MATLAB expression (or recipe) for creating an (n + 1) -dimensional (m 1 × m 2 × × & hellip; × m n & times; k) an array obtained by "stacking" k n -dimensional arrays f(b) , where the parameter B is in the range B
To do this in numpy , I would use an expression like this:
C = concatenate([f(b)[..., None] for b in B], -1)
In the case of its use, I will unpack this numerical expression below (see APPENDIX ), but its feature, which I want to emphasize now, is that it is completely agnostic for figures / sizes f(b) and B For the types of applications that I have in mind, the ability to write such “formal agnostic” code is of paramount importance. (I emphasize this point because a lot of the MATLAB code that I come across for this kind of manipulation is clearly not “formally agnostic,” and I don't know how to do it.)
application
In the general case, if A is a numpy array, the expression A[..., None] can be considered as a “change of form" of A , so that it takes one additional, trivial dimension. Thus, if f(b) is n-dimensional (m 1 · m 2 · hellip; × m n ), then f(b)[..., None] is corresponding (n + 1) - dimensional (m 1 x m 2 x? hellip; m n x 1) array. (The reason for adding this trivial dimension will become apparent below.)
Given this clarification, the meaning of the first concatenate argument, namely:
[f(b)[..., None] for b in B]
not too hard to decrypt. This is a standard Python list comprehension, and it evaluates a sequence of k (n + 1) -dimensional (m 1 x m 2 x & hellip; x m n x 1) arrays f(b)[..., None] , since the parameter B runs through the vector B
The second argument to concatenate is the "axis" along which concatenation should be performed, expressed as an index of the corresponding dimension of the arrays to be combined. In this context, the -1 index plays the same role as the end keyword in MATLAB. Therefore, the expression
concatenate([f(b)[..., None] for b in B], -1)
says "combine the arrays f(b)[..., None] along their last dimension." This means that this “last dimension” is concatenated by the need to change the array f(b) (for example, f(b)[..., None] ).