How to convert data types in Python to mlarray?
4 ビュー (過去 30 日間)
古いコメントを表示
import matlab.engine
eng = matlab.engine.start_matlab()
# mlarray to ndarray: np.asarray(x._data, dtype=dtype)
c = eng.magic(5)
print(type(c))
d = np.asarray(c)
print(type(d))
# ndarray to mlarray:?????
a = np.random.random(10)
a1 = a.tolist()
a2 = eng.cell(a1)
a3 = eng.cellfun(@double,a2,'UniformOutput',false)
print(type(a1))
print(type(a2))
print(type(a3))
SyntaxError: invalid syntax
How to convert data types in Python to mlarray?
0 件のコメント
回答 (1 件)
rakshit gupta
2023 年 6 月 6 日
To convert a ndarray to a mlarray in Python using MATLAB Engine, you can use the 'matlab.double()' function.
Here's an example of how you can convert an ndarray to mlarray:
import matlab.engine
import numpy as np
eng = matlab.engine.start_matlab()
# create an ndarray
a = np.random.random(10)
# convert ndarray to mlarray
mla = matlab.double(a.tolist())
# verify that the mlarray was created
print(type(mla))
# pass the mlarray to a MATLAB function, for example, to calculate the sum
result = eng.sum(mla)
# print the result
print(result)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Call MATLAB from Python についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!