How can I convert Python list to column vector?

7 次查看(过去 30 天)
I'm using the fitdist method from MATLAB in Python where x is:
I tried different approaches to work on this and they all give the same error:
eng.fitdist(eng.cell2mat(list(x)), 'stable')
eng.fitdist(matlab.double(list(x)), 'stable')
eng.fitdist(list(x), 'stable')
All of these give me this error:
MatlabExecutionError:
File C:\Program Files\MATLAB\R2020b\toolbox\stats\stats\fitdist.m, line 126, in fitdist X must be a numeric column vector.
Any idea how to get out of it? How do I convert my list to a column vector that works with MATLAB?
I am using MATLAB R2020b

采纳的回答

Natali Ayoub
Natali Ayoub 2021-2-24
编辑:Natali Ayoub 2021-2-24
For anyone running into this, below is what resolved it for me:
"You may use the size argument of matlab.double for creating a column vector.
Column vector in MATLAB is equivalent to 2D matrix with second dimension size equals 1.
For example: A Matrix with size [5, 1] is a column vector with 5 columns.
According to the documentation, MATLAB Arrays as Python Variables:
matlab.double has an optional size argument:
matlab.double(initializer=None, size=None, is_complex=False)
You can set size argument to (x.size, 1) for creating a column vector.
The following syntax works (assuming x is a NumPy array):
eng.fitdist(matlab.double(list(x), (x.size, 1)), 'stable')
The following syntax also works:
eng.fitdist(matlab.double(list(x), (len(list(x)), 1)), 'stable')
The following code was used for testing:
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
x = np.array([176.0, 163.0, 131.0, 133.0, 119.0, 142.0, 142.0, 180.0, 183.0, 132.0, 128.0, 137.0, 174.0])
eng.fitdist(matlab.double(list(x), (x.size, 1)), 'stable')
"
Source:
https://stackoverflow.com/questions/66247949/how-can-i-convert-python-list-to-column-vector

更多回答(1 个)

Raynier Suresh
Raynier Suresh 2021-2-22
Hi, The input to fitdist must be a column vector, from your image it looks like the input has multiple columns in it. You can pass the list with single column to matlab.double() and then pass it to the fitdist function. You can refer the below code and links for more information.
from sklearn import datasets
import matlab.engine
eng = matlab.engine.start_matlab()
iris = datasets.load_iris()
X = iris.data[:, :1]
print(X)
L = X.tolist()
print(L)
Pd = eng.fitdist(matlab.double(L),'stable')
print(Pd)
  1 个评论
Natali Ayoub
Natali Ayoub 2021-2-24
Hi, thank you for getting back to me.
My x is only sepal length and the line of code below does exactly what you suggested:
eng.fitdist(matlab.double(list(x)), 'stable')
Which did not resolve my issue.
I posted the same question on stackoverflow and their response resolved my issue
https://stackoverflow.com/questions/66247949/how-can-i-convert-python-list-to-column-vector

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by