![]() P.s: Edit tags as appropriate, I added as many as made sense to me. I know about permutation matrices, but they only permute entire rows and columns not individual entries.įor example: Say I want to permute $x_ Use the Permute Matrix block to permute a matrix by row or column. I want to permute two entries in $A$, any two entries as needed: In general, for any two entries $a_j,b_k$ in the matrix is it possible to do this with some matrix $B$ dependent on $a_j,b_k$? There's no check for this (this would obscure the code) but apart from this it works well.Let $A$ be an $8 \times 8$ matrix with integer coefficients. The lengths of these dimensions should be equal, of course. Here A and B are the tensors to be contracted along the dimesions i and j respectively. For example, permute(A,2 1) switches the. Here are my subroutines for the first and second tasks:ġ) tensor product: function = tensor(A,B)Ĭ = squeeze( reshape( repmat(A(:), 1, numel(B)).*B(:).', ) ) B permute( A, dimorder ) rearranges the dimensions of an array in the order specified by the vector dimorder. I would like to share my answer to the problems of:ġ) making the tensor product of two tensors (of any valence) Ģ) making the contraction of two tensors along any dimension. # 2 experiments measuring 3 signals at nT timestamps C ipermute (H, 3 1 2) I need to separate the permuted vectors and change it back into separate planes and then finally recombine the planes to get a 3 dimensional image.The following is the code that I used to permute the three planes. Examples The following examples illustrate some of the functions mentioned above. ndmult, by ajuanpi (Juan Pablo Carbajal), 2013, GNU GPL MATLAB provides the following functions to sort, rotate, permute, reshape, or shift array contents.To answer the question, and for readability, please see: There may not be an approach that is optimal in all cases, but the loop is still pretty good, and best in many cases. So (2) wins by a big factor, in this (maybe extreme) case. To permute the columns, the permutation matrix is p x p, and the matrix multiplication requires O ( Np 2) operations. Here, manipulate means replicating and rotating arrays or parts of arrays, inserting, extracting, permut- ing and shifting elements, generating combinations and permutations of elements, run- length encoding and decoding, multiplying and dividing arrays and calculating distance matrics and so forth. However, if one tests the memory usage of a multidimensional permutation, it's the same as the variable used. % pretty big data.īut the loop method can be slower than (2), if the looped dimension is much larger than the others. The permutation operation needs to output a different matrix to the output, it's not like reshape, where the data is not modified, permute does modify the data. But with much bigger data, (3) beats (2). These differences become more dramatic with larger data. Note that the times can vary quite a lot from one trial to another, and sometimes (2) comes out the slowest. There are better ways of computing the Euclidean distances between sets of points. % - Approach 3: cellfun (via = x*Y,num2cell(X,),'UniformOutput',false) Īll three approaches produced the same output (phew!), but, surprisingly, the loop was the fastest: Elapsed time is 0.000418 seconds. For a large sparse matrix it would not be unusual to see iterative size increasing code to take days or even weeks of computation time. % - Approach 2: Reshape Permute (via = reshape(reshape(permute(X, ), ), )' * Y % - Approach 1: Loop (via = zeros(A,D,C) My intuition was that (2) would be the winner. ![]() I recently compared all three methods to see which was quickest. E.g. There are roughly three approaches that i see around, short of using outside libraries (i.e., mtimesx): permute(Y, 1 3 2 4 ) reshape(X, m n ) 6.1.2 Create3Darray(columnsrst) Assumeyouwanttocreatea p-by-q-by-mn/(pq)arrayYwherethei,jsubmatrixof XisY(:,:,i (j-1)m/p). I'm approaching the exact same issue, with an eye for the most efficient method.
0 Comments
Leave a Reply. |