2017-10-31 1 views
1

Ich habe testPython.py:Wie man ein numpy Array in eine Matlab Matrix konvertiert?

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

und ich habe test.m:

clear; clc; 

value = py.testPython.numpyTest() 

Als ich test.m laufen erhalte ich:

Testing 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] 

Wie konvertiere ich Python-Numpy-Array in Matlab-Matrix?

+0

Warum in Python nicht ganz funktionieren? (https://Stackoverflow.com/a/17535694/1959808) Welchen Nutzen hat Matlab? –

+0

@IoannisFilippidis der einzige Grund für die Verwendung von Matlab ist, weil ich Matlab-Code habe, der ROS und Gazebo steuert. Ich möchte Deep-Learning-Tools wie Tensorflow und Pytorch verwenden UND das mit Gazebo und ROS über Matlab verbinden – MoneyBall

+0

hast du versucht, auf "value" zuzugreifen? z.B. 'Wert (1)' –

Antwort

0

Wir müssen einige Umwandlungen durchführen hier:

clear; clc; 

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

Eine weitere Option ist es, Daten über Datei zu übertragen, aber ich mag es nicht.

und ein Link zu Matlab Docs über Python-Liste Umwandlung:

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

Verwandte Themen