Problem with matrices between Python and Matlab

조회 수: 12 (최근 30일)
Gautier
Gautier 2023년 5월 24일
댓글: Gautier 2023년 5월 25일
Hi everyone,
I'm starting with matlab and I would like tu use a matlab function in python (gflineq) to solve a linear equation. However, it doesn't work as matrices are not represented the same way in python and matlab. How can I edit my program in order to make it work ?
Thank you !!
  댓글 수: 2
Rik
Rik 2023년 5월 25일
Did you try creating the matrix as a vector and using reshape to make it the correct dimensions?
Gautier
Gautier 2023년 5월 25일
Yes I tried but the problem was the same for vectors.

댓글을 달려면 로그인하십시오.

채택된 답변

Karthik
Karthik 2023년 5월 25일
Hello Gautier,
I Understand that you are trying to use MATLAB "gflineq" in python and are facing issues passing NumPy/Python based arrays to this function.
The following Example Code shows how we can pass python arrays to MATLAB gflineq function.
import matlab.engine
import numpy as np
# Start MATLAB engine
eng = matlab.engine.start_matlab()
# Define the matrix A and the vector b
A = np.matrix([[1,0,0],
[1,1,1],
[1,1,1],
[0,1,1]])
b = matlab.double([[1], [0], [0], [1]]) # Make b a column vector
# Convert A to MATLAB's double type
A = matlab.double(A.tolist())
x= eng.gflineq(A, b)
# Convert the output to NumPy arrays
x = np.array(x)
# Display the results
print("x =", x)
You can refer to the following links for more details:
Thanks,
Karthik.
  댓글 수: 1
Gautier
Gautier 2023년 5월 25일
Thank you very much it helps me a lot 😊 Have a nice day !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by