フィルターのクリア

Can you use .toarray() for to transform matlab.double objects to python arrays?

15 ビュー (過去 30 日間)
Anna Varbella
Anna Varbella 2022 年 12 月 2 日
コメント済み: Anna Varbella 2022 年 12 月 7 日
I have to transform N, a matlab.double object, size 118x4, to an python array in the fastest way possible, can you use the .toarray() function?
Otherwise I am doing numpy.asarray(N._data) which is fast, but not the best option.

回答 (1 件)

Kunal Kandhari
Kunal Kandhari 2022 年 12 月 7 日
There are a couple of ways to accomplish this. One method is to use numpy's built-in method, 'asarray':
Start by loading your matlab.double array:
myData = eng.eval("load('{}','cluster_class','par')".format('sampleData.mat'))
With MATLAB R2022a and later, you can convert matlab.double directly to a numpy array:
a = np.array(myData['cluster_class'])
To add additional specification, use MATLAB engine's functions to convert to a Python array with 'noncomplex()', then to a numpy array:
a = np.array(myData['cluster_class'].noncomplex().toarray(), 'int')
We use the 'noncomplex()' call to retrieve the data in 1D format (provided it's noncomplex; for complex data, we can use 'real()' and 'imag()' calls). We also offer the 'toarray()' call to convert a 1D matlab.double into a Python array.array object.
  1 件のコメント
Anna Varbella
Anna Varbella 2022 年 12 月 7 日
Thank you for you answer! However I'm not trying to load a saved .mat file, but I already get my data in python as a matlab double, saving and loading would slow down a lot my program.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by