필터 지우기
필터 지우기

python中如何将​matlab.dou​ble转化为tens​or

조회 수: 15 (최근 30일)
qiao cheng
qiao cheng 2022년 8월 30일
답변: Dinesh 2023년 6월 6일
eng = matlab.engine.start_matlab() # 可以调用matlab的内置函数。
output = eng.test(indata)
上述代码中output类型为matlab.double,如果想要将其转化为pytorch中的tensor类型,该怎么做?

답변 (1개)

Dinesh
Dinesh 2023년 6월 6일
Hi quio cheng!
To convert a 'matlab.double' variable to a 'Tensor' in Python, you can use the 'numpy' library to first convert the matlab.double to a NumPy array, and then convert the 'NumPy array' to a 'Tensor'.
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'])
if you are using pytorch then
import torch
x_tensor = torch.tensor(a)
if you are using tensorflow then
import tensorflow as tf
data_tf = tf.convert_to_tensor(a)
Hope this helps!
Thank you.

카테고리

Help CenterFile Exchange에서 MATLAB 中的 Python 库에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!