【问题标题】:How to convert numpy array to Matlab matrix?如何将numpy数组转换为Matlab矩阵?
【发布时间】:2017-10-31 10:53:28
【问题描述】:

我有 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 矩阵?

【问题讨论】:

  • 为什么不完全在 Python 中工作? (stackoverflow.com/a/17535694/1959808) 使用 Matlab 有什么好处?
  • @IoannisFilippidis 使用 matlab 的唯一原因是因为我有控制 ROS 和凉亭的 matlab 代码。我想使用 tensorflow 和 pytorch 等深度学习工具,并通过 matlab 将其连接到凉亭和 ROS
  • 您是否尝试过访问value?例如value(1)
  • @AnderBiguri 是的。它不起作用,因为它是 python ndarray。
  • 一定有什么,你显示的输出最后清楚地显示[1 2 3]

标签: python arrays matlab numpy matrix


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多