python - 如何将numpy数组转换为Matlab矩阵?

标签 python arrays matlab numpy matrix

我有 testPython.py :

import numpy as np
def numpyTest():
    print("Testing numpy...")
    x = np.array([1,2,3])
    return x

我有 test.m:

clear; clc;

value = py.testPython.numpyTest()

当我运行 test.m 时,我得到:

测试 numpy...

value = 

  Python ndarray with properties:

           T: [1×1 py.numpy.ndarray]
        base: [1×1 py.NoneType]
      ctypes: [1×1 py.numpy.core._internal._ctypes]
        data: [1×24 py.buffer]
       dtype: [1×1 py.numpy.dtype]
       flags: [1×1 py.numpy.flagsobj]
        flat: [1×1 py.numpy.flatiter]
        imag: [1×1 py.numpy.ndarray]
    itemsize: 8
      nbytes: 24
        ndim: 1
        real: [1×1 py.numpy.ndarray]
       shape: [1×1 py.tuple]
        size: 3
     strides: [1×1 py.tuple]

    [1 2 3]

如何将 python numpy 数组转换为 matlab 矩阵?

最佳答案

我们必须在这里执行一些转换:

clear; clc;

value = py.testPython.numpyTest();% ndarray
pyList = value.tolist(); % ndarray -> pylist
result = cellfun(@double,cell(pyList)) %pylist-> cell array -> double array

另一种选择是通过文件传输数据,但我不喜欢它。

以及有关 Python 列表转换的 Matlab 文档的链接:

https://www.mathworks.com/help/matlab/matlab_external/use-python-list-of-numeric-types-in-matlab.html

关于python - 如何将numpy数组转换为Matlab矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033614/

相关文章:

python - Django 如何防止接受 future 日期?

python - 如何将 Pandas 中的坐标从 NAD83 转换为常规 GPS 坐标(纬度/经度)?

java - 验证二维数组是否有两个相等行的算法

c# - 结构数组示例

matlab - 从矩阵中移除 NaN 元素

python - 创建对象而不调用类

python - python项目结构是什么

python - 获取当前类(class)的名称?

arrays - Julia:用矩阵追加矩阵数组

multithreading - 如何连续从Matlab的串口读取?