Error in converting ndarray to double

When I write in MATLAB,
y = py.numpy.array(1:10);
I receive ndarray but I am unable to convert it into double for further use like following line
x=2*double(y)
Error using double
Conversion to double from py.numpy.ndarray is not possible.

댓글 수: 3

Ameer Hamza
Ameer Hamza 2020년 10월 22일
Which MATLAB release are you using?
Sajid Sarwar
Sajid Sarwar 2020년 10월 22일
MATLAB 2017a
Suyash Sule
Suyash Sule 2020년 11월 3일
I am experiencing the same error on Matlab 2020a as well. A few online answers suggest that double() should work but it doesnt. double(np.array.array()) also does not work. Please help.

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

답변 (2개)

Michael Loibl
Michael Loibl 2021년 12월 7일

3 개 추천

I actually have the same problem. Now, I found a nasty (but in my case helpful) solution. I am using the fact that I am able to convert Python lists to Matlab. I did the following type conversions:
y_py=py.numpy.array(1:10);
y_py_list=y.tolist();
y_mat_cell=cell(y_py_list);
y_mat=cell2mat(y_mat_cell);
Nevertheless, it is also unclear to me, why the conversion seems to work that easily for other people. Therefore, I would be happy to see further more sofisticated and more general answers.

댓글 수: 4

To convert two-dimensional numpy-arrays with the same nasty solution, you can use the following code:
npArray = py.numpy.random.rand(3,3)
pyList = npArray.tolist();
% List to cell
matCell = cell(pyList)';
for i = 1:length(matCell)
if isa(matCell{i}, 'py.list')
matCell{i} = cell2mat(cell(matCell{i}));
end
end
mat = cell2mat(matCell);
Lucademicus
Lucademicus 2023년 11월 17일
Suddenly my code stopped working, while it worked yesterday. I get the same error
> Error using double
> Conversion to double from py.numpy.ndarray is not possible.
I'm in R2023b so this mystery is still there.
Lucademicus
Lucademicus 2023년 12월 1일
A restart of MATLAB made it work again. Not sure what happened, but perhaps this helps anybody
Petri M
Petri M 2025년 12월 9일
Matlab version 2025b and Python 3.13, having the same problem: double(...) does not work.
But this tolist conversion works, so still thanks to Michael !

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

Chris H
Chris H 2023년 11월 28일

0 개 추천

I experienced exactly this issue when running MATLAB R2023a on Linux (RHEL 8.8), when using a python3.9 install via miniconda.
I do not know why using the python 3.9 install from miniconda created this issue, but when I deactivated the conda environment (which I had set to my default environment via my bashrc) and installed python39 and python39-numpy via yum/dnf, suddenly everything started working. You can keep your default conda environment outside of MATLAB and then within MATLAB select the python3.9 installed at the OS level via the command:
pyenv(Version='/usr/bin/python3.9')
(so long as python is installed at that location)

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

질문:

2020년 10월 22일

댓글:

2025년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by