python - 在 Matlab 排序中从 numpy 数组顺序获取数据

标签 python matlab numpy

举个例子,假设在 Matlab 中,矩阵 a(2,3,2) 如下所示:

a(:,:,1) =

     1     2     3
     4     5     6


a(:,:,2) =

     7     8     9
    10    11    12

如果我使用 mex 并按顺序访问此矩阵的元素,我会得到以下顺序(最后,按顺序访问它们的代码):

1, 4, 2, 5, 3, 6, 7, 10, 8, 11, 9, 12

现在,如果我在 numpy 中有相同的矩阵

In [2]: np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
Out[2]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

,我可以尝试使用 .flatten(...) 顺序访问项目(找不到更好的方法 - 我愿意接受建议)。 flatten() 有 4 个“排序”选项:

In [4]: a.flatten('F')
Out[4]: array([ 1,  7,  4, 10,  2,  8,  5, 11,  3,  9,  6, 12])

In [5]: a.flatten('C')
Out[5]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

In [6]: a.flatten('A')
Out[6]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

In [7]: a.flatten('K')
Out[7]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

有没有一种优雅的方法可以在 Matlab 排序中访问 numpy 数组的元素? (在我的真实用例中,这些矩阵很大,所以复制它们并不是首选)

附录:顺序打印矩阵的代码

[不太好,我知道,只是为了测试]

  1 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  2 {
  3         // This just a test: hardcoding the size of `data_input`
  4         int data_input_size = 12;
  5         double *data_input;
  6         const mxArray *batch_data;
  7 
  8         // Gets a pointer to the first element of batch_data
  9         data_input  = mxGetPr(prhs[0]);
 10 
 11         for(int i = 0; i < data_input_size; i++) {
 12                 printf("working_data[i]: %f\n", data_input[i]);
 13         }
 14         plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
 15 }

最佳答案

这让很多从 MATLAB 进入 NumPy/Python 的人感到困惑。因此,在 MATLAB 中,索引格式为 (column x row x dim3) 等等。对于 NumPy,它是 (axis-0, axis-1, axis-2) 等等。

使用 MATLAB 上的示例案例示意性地展示这一点:

>> a = reshape(1:27,[3,3,3]);
>> a
a(:,:,1) =

        row
    --------------->
     1     4     7   |         |
     2     5     8   | col     |
     3     6     9   v         |
a(:,:,2) =                     |
    10    13    16             | dim3
    11    14    17             |
    12    15    18             |
a(:,:,3) =                     |
    19    22    25             |
    20    23    26             |
    21    24    27             v

在 NumPy 上:

In [62]: a = np.arange(27).reshape(3,3,3)

In [63]: a
Out[63]: 

            axis=2
         ---------->
array([[[ 0,  1,  2],   |          |
        [ 3,  4,  5],   | axis=1   |
        [ 6,  7,  8]],  v          |
                                   |
       [[ 9, 10, 11],              |
        [12, 13, 14],              | axis=0
        [15, 16, 17]],             |
                                   |
       [[18, 19, 20],              |
        [21, 22, 23],              |
        [24, 25, 26]]])            v

让我们尝试将问题中列出的 3D 数组案例关联到这两个 环境 之间的维度和轴术语:

MATLAB      NumPy
------------------
cols        axis-1
rows        axis-2
dim3        axis-0

因此,要在 NumPy 中模拟与 MATLAB 相同的行为,我们需要 NumPy 中的轴:(1,2,0)。结合 NumPy 从最后一个轴到第一个轴存储元素的方式,即以相反的顺序,所需的轴顺序将是 (0,2,1)

要以这种方式执行轴排列,我们可以使用 np.transpose然后使用 np.ravel() 进行展平操作-

a.transpose(0,2,1).ravel()

sample 运行-

In [515]: a
Out[515]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

In [516]: a.transpose(0,2,1) # Permute axes
Out[516]: 
array([[[ 1,  4],
        [ 2,  5],
        [ 3,  6]],

       [[ 7, 10],
        [ 8, 11],
        [ 9, 12]]])

In [517]: a.transpose(0,2,1).ravel() # Flattened array
Out[517]: array([ 1,  4,  2,  5,  3,  6,  7, 10,  8, 11,  9, 12])

关于python - 在 Matlab 排序中从 numpy 数组顺序获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41507430/

相关文章:

c - 当我在 MATLAB 中单击 UI 按钮时,我需要交替使用 X 和 O

python - 如何使我的 pylab.poly1d(fit) 通过零?

python - SQLAlchemy select_from 单个表

python - Tensorflow 通过 eval_metric_ops 在 Tensorboard 中绘制 tf.metrics. precision_at_thresholds

MATLAB:函数进行 4 次递归调用。我有一个 4 核处理器。我可以并行化吗?

python - numpy.argmax 比 MATLAB [~,idx] = max() 慢吗?

Python Flask-Restful 应用程序与 Kubernetes - 连接被拒绝

python - Django 1.7 长期迁移永无止境

python - 如何加速矩阵码

python - 使用 numpy 随机选择条目的快速方法