aperm方法


aperm方法
Transpose an array by permuting its dimensions and optionally resizing it.
aperm(a, perm, ...)
a   
the array to be transposed.
perm   
the subscript(下标) permutation vector, usually a permutation(组合) of the integers 1:n, where n is the number of dimensions of a. When a has named dimnames, it can be a character vector of length n giving a permutation of those names. The default (used whenever perm has zero length) is to reverse the order of the dimensions.
permutation
置换;排列(方式);组合(方式)
交换;变换;彻底改变
  1. ><- array(1:24, 2:4)
  2. > x
  3. , , 1
  4.  
  5.      [,1] [,2] [,3]
  6. [1,]    1    3    5
  7. [2,]    2    4    6
  8.  
  9. , , 2
  10.  
  11.      [,1] [,2] [,3]
  12. [1,]    7    9   11
  13. [2,]    8   10   12
  14.  
  15. , , 3
  16.  
  17.      [,1] [,2] [,3]
  18. [1,]   13   15   17
  19. [2,]   14   16   18
  20.  
  21. , , 4
  22.  
  23.      [,1] [,2] [,3]
  24. [1,]   19   21   23
  25. [2,]   20   22   24

  26. > xt <- aperm(x, c(2,1,3))
  27. > xt
  28. , , 1
  29.  
  30.      [,1] [,2]
  31. [1,]    1    2
  32. [2,]    3    4
  33. [3,]    5    6
  34.  
  35. , , 2
  36.  
  37.      [,1] [,2]
  38. [1,]    7    8
  39. [2,]    9   10
  40. [3,]   11   12
  41.  
  42. , , 3
  43.  
  44.      [,1] [,2]
  45. [1,]   13   14
  46. [2,]   15   16
  47. [3,]   17   18
  48.  
  49. , , 4
  50.  
  51.      [,1] [,2]
  52. [1,]   19   20
  53. [2,]   21   22
  54. [3,]   23   24
  55.  

让我来解释下perm的意思吧
原来的数组的维度是行,列,层分别为2,3,4
现在perm=2,1,3  
2的意思是原来下标是2的那个数(排在第二个的参数),现在排在第一位(行
1的意思是原来下标是1的那个数,现在排在第2位(列)
3的意思是原来下标是3的那个数,现在排在第三位(层)
所以perm=2,1,3的等价于(3,2,4)






原文地址:https://www.cnblogs.com/xuanlvshu/p/5297649.html