How to create a 3D Matrix?
26 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Stephen23
2023-1-27
编辑:Stephen23
2023-1-27
"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])
size(a)
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.
0 个评论
Al Danial
2023-1-29
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 个评论
Al Danial
2023-2-8
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!