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

3 Ansichten (letzte 30 Tage)
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 Kommentar
Matt J
Matt J am 3 Aug. 2022
Bearbeitet: Matt J am 3 Aug. 2022
Python lists are always 1D, I believe. Only NumPy arrays have an actual shape.
Why would it help to make the list 3D?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Al Danial
Al Danial am 5 Aug. 2022
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

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by