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

조회 수: 9 (최근 30일)
Julia Gorman
Julia Gorman 2022년 8월 3일
답변: Al Danial 2022년 8월 5일
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