How to create a 3D Matrix?

18 views (last 30 days)
Shweta Kiran
Shweta Kiran on 27 Jan 2023
Commented: Shweta Kiran on 8 Feb 2023
I have a 3D matrix generated from python. When I copy the matrix in MATLAB, it is instead detected as a 2D matrix. Is there a way which can make the matrix 3D just like it was in MATLAB.
One sample of how the matrix looks like is as follows:
a= [[[-0.2329, -0.0547, -0.2237, 0.0131, -0.1883],
[-0.2552, -0.1730, -0.2636, -0.2292, -0.0756]],
[[ 0.2776, 0.0300, 0.0741, 0.1021, -0.1450],
[-0.2825, -0.3447, -0.2900, -0.1007, -0.0216]],
[[ 0.2355, -0.1435, -0.2841, 0.0641, 0.1605],
[ 0.2214, -0.2439, 0.1734, -0.3350, 0.1692]]];
While in python this matrix is of 3*2*5, in MATLAB, it becomes 6*5.

Answers (2)

Stephen23
Stephen23 on 27 Jan 2023
Edited: Stephen23 on 27 Jan 2023
"While in python this matrix is of 3*2*5,"
Not really, it is actually just a whole lot of nested 1D lists. In some languages these might be called a "matrix" or "3D array", but they are just deluding themselves with some nested lists. Note that there is nothing stopping you from making all of those lists different lengths (i.e. so-called "jagged array").
In contrast, MATLAB (and numpy) store actual contiguous arrays in memory, that means one single block of numeric data with a specific size. And just like mathematical arrays they must have consistent dimension sizes. You can concatenate a 3D array out 2D matrices by using the CAT() function:
a = cat(3,...
[-0.2329, -0.0547, -0.2237, 0.0131, -0.1883;...
-0.2552, -0.1730, -0.2636, -0.2292, -0.0756],...
[0.2776, 0.0300, 0.0741, 0.1021, -0.1450;...
-0.2825, -0.3447, -0.2900, -0.1007, -0.0216],...
[0.2355, -0.1435, -0.2841, 0.0641, 0.1605;...
0.2214, -0.2439, 0.1734, -0.3350, 0.1692])
a =
a(:,:,1) = -0.2329 -0.0547 -0.2237 0.0131 -0.1883 -0.2552 -0.1730 -0.2636 -0.2292 -0.0756 a(:,:,2) = 0.2776 0.0300 0.0741 0.1021 -0.1450 -0.2825 -0.3447 -0.2900 -0.1007 -0.0216 a(:,:,3) = 0.2355 -0.1435 -0.2841 0.0641 0.1605 0.2214 -0.2439 0.1734 -0.3350 0.1692
size(a)
ans = 1×3
2 5 3
Note that unlike Python where square brackets define a list, in MATLAB square brackets are a concatenation operator. So your attempt is perfectly valid MATLAB code (even if rather obfuscated) that simply concatenates three matrices into one matrix. How to create vectors and matrices using concatenations is explained in the MATLAB introductory tutorials:
I strongly recommend doing these tutorials: MATLAB is not Python, and you will not get far pretending that it is.

Al Danial
Al Danial on 29 Jan 2023
As @Stephen23 mentioned, your expression of "a = " above is a Python list. If instead you return it to matlab as a NumPy array, matlab will see it as a 3D matrix. Note: NumPy and matlab use different methods to store matrix terms in memory--matlab uses column major while NumPy uses row major storage. This gets quite complicated for 3D matrices; you may have to reshape and transpose to get terms in the arrangement you expect.
Aside from that, say your Python code is in the file get_array.py:
import numpy as np
# get_array.py
def x():
a= [[[-0.2329, -0.0547, -0.2237, 0.0131, -0.1883],
[-0.2552, -0.1730, -0.2636, -0.2292, -0.0756]],
[[ 0.2776, 0.0300, 0.0741, 0.1021, -0.1450],
[-0.2825, -0.3447, -0.2900, -0.1007, -0.0216]],
[[ 0.2355, -0.1435, -0.2841, 0.0641, 0.1605],
[ 0.2214, -0.2439, 0.1734, -0.3350, 0.1692]]]
x = np.array(a) # convert list to NumPy array
return x
You can get the 3D matrix in matlab with
>> get_array = py.importlib.import_module('get_array');
>> y = double( get_array.x() );
>> whos y
Name Size Bytes Class Attributes
y 3x2x5 240 double
  3 Comments
Al Danial
Al Danial on 8 Feb 2023
Which version of matlab are you running? Only versions 2021b and newer support Python 3.9. See the compatibility table https://www.mathworks.com/support/requirements/python-compatibility.html
Shweta Kiran
Shweta Kiran on 8 Feb 2023
I am using R2021b and Python 3.9.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!