Why does MATLAB's permute not need extra memory?

前端 未结 2 2240
再見小時候
再見小時候 2021-02-19 04:17

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

2条回答
  •  佛祖请我去吃肉
    2021-02-19 04:28

    This is only a guess, as I don't really know what permute does under the hood.

    1. Permuting of dimensions can always be done as a sequence of elementary permute operations, where" elementary" means interchanging only two dimensions. For example, permute(x, [4 1 2 3]) is equivalent to this sequence of elementary permute operations (the sequence is not unique):

      permute(..., [4 2 3 1]) %// interchange dims 1 and 4: we have dims [4 2 3 1]
      permute(..., [1 4 3 2]) %// interchange dims 2 and 4: we have dims [4 1 3 2]
      permute(..., [1 2 4 3]) %// interchange dims 3 and 4: we have dims [4 1 2 3]
      
    2. Each of these elementary operations (interchanging two dimensions) is essentially a transpose along a given plane of the multidimensional array, performed repeatedly along all other dimensions.

    3. Transposition can be done inline, requiring only a fixed amount of extra memory, independent of array size, or growing very slowly with array size.

    Putting these three facts together shows that it is possible to do it without a significant amount of extra memory. This may not be the approach actually used by Matlab, though.

提交回复
热议问题