MATLAB Answers

How can I convert Python list to column vector?

20 views (last 30 days)
Natali Ayoub
Natali Ayoub on 17 Feb 2021
Edited: Natali Ayoub on 24 Feb 2021
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:
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

Accepted Answer

Natali Ayoub
Natali Ayoub on 24 Feb 2021
Edited: Natali Ayoub on 24 Feb 2021
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')

More Answers (1)

Raynier Suresh
Raynier Suresh on 22 Feb 2021
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 =[:, :1]
L = X.tolist()
Pd = eng.fitdist(matlab.double(L),'stable')
  1 Comment
Natali Ayoub
Natali Ayoub on 24 Feb 2021
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!