I have a variable named mesh
of type object
extracted from a MATLAB .mat file. (EDIT: Reproducible example below)
In [1]: mesh
Out[1]:
array([[ array([[ (array([[ 89, 108]], dtype=uint8),
array([[-131.659809],
[-131.659809],
[-131.659809],
...,
[ 52.022239],
[ 52.022239],
[ 52.022239]]),
array([[ 189.358345],
[ 187.271049],
[ 185.183753],
...,
[ -29.807736],
[ -31.895032],
[ -33.982328]]))]],
dtype=[('dim', 'O'), ('x', 'O'), ('y', 'O')])]], dtype=object)
How can I access the individual arrays dim
, x
and y
?
Reproducible example:
I am unable to assign dtype=uint8
to an array as in the imported object.
Also, the imported object has object size (1,1) while this example has object size (1,1,1,1).
import numpy as np
mesh = np.array([[ np.array([[ (np.array([[ 6, 6]], dtype=float),
np.array([[-131.659809],
[-131.659809],
[-131.659809],
[ 52.022239],
[ 52.022239],
[ 52.022239]]),
np.array([[ 189.358345],
[ 187.271049],
[ 185.183753],
[ -29.807736],
[ -31.895032],
[ -33.982328]]))]],
dtype=[('dim', 'O'), ('x', 'O'), ('y', 'O')])]], dtype=object)
To access the separate arrays above, one could do the following:
>>> mesh[0][0][0][0][0] # dim
array([[ 6., 6.]])
>>> mesh[0][0][0][0][1] # x
array([[-131.659809],
[-131.659809],
[-131.659809],
[ 52.022239],
[ 52.022239],
[ 52.022239]])
>>> mesh[0][0][0][0][2] # y
array([[ 189.358345],
[ 187.271049],
[ 185.183753],
[ -29.807736],
[ -31.895032],
[ -33.982328]])
And to set a value, one could for example do:
>>> mesh[0][0][0][0][0][0][0]=1
>>> mesh
array([[[[ (array([[ 1., 6.]]), array([[-131.659809],
[-131.659809],
[-131.659809],
[ 52.022239],
[ 52.022239],
[ 52.022239]]), array([[ 189.358345],
[ 187.271049],
[ 185.183753],
[ -29.807736],
[ -31.895032],
[ -33.982328]]))]]]], dtype=object)
This answer is a hack, not a solution. It is not meant to be interpreted as a general answer, but rather is specific to the exact example above. It does not deal with numpy, just with being able to access the specific arrays.