How to convert a 1x5x5600 cell in MATLAB to a python list that is (1,5,5600)

5 次查看(过去 30 天)
I have a cell in MATLAB is 1x5x5600 and I need to convert it so I can use it in python and it would be easiest if it were in a list that is dimensions (1,5,5600)
  1 个评论
Matt J
Matt J 2022-8-3
编辑:Matt J 2022-8-3
Python lists are always 1D, I believe. Only NumPy arrays have an actual shape.
Why would it help to make the list 3D?

请先登录,再进行评论。

回答(1 个)

Al Danial
Al Danial 2022-8-5
Lists, like cell arrays, may be nested so @J C's request is doable. Here's an example with a 1x3x5 cell:
% create the cell array
a = cell(1,3,5);
for r = 1:3
for c = 1:5
a{1,r,c} = r*1000 + c;
end
end
% convert it to an equivalent, similarly shaped Python list
py_a = py.list();
py_a_level1 = py.list();
for r = 1:3
py_a_level2 = py.list();
for c = 1:5
py_a_level2.append( a{1,r,c} );
end
py_a_level1.append( py_a_level2 );
end
py_a.append( py_a_level1 );
In Python, py_a looks like this:
In : py_a
Out:
[[[1001.0, 1002.0, 1003.0, 1004.0, 1005.0],
[2001.0, 2002.0, 2003.0, 2004.0, 2005.0],
[3001.0, 3002.0, 3003.0, 3004.0, 3005.0]]]
In : py_a[0][1][2]
Out: 2003.0

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by