Array stored in numpy void returns no shape/length

Evan Maltz

I have a large numpy void array loaded from a matlab struct. I can access the arrays stored inside the main array, but accessing their shape returns '()'. Accessing their length returns the error: len() of unsized object. Is there a way for me to access the shape of the sub-arrays?

arr = scipy.io.loadmat('mymatfile.mat', squeeze_me=True)
sub_arr = arr['a'][0]['b']['c']
print(sub_arr)
print(sub_arr.shape)
print(len(sub_arr))

returns: [[0 0 0 0] [0 0 0 0]] ()

TypeError: len() of unsized object

Solution: sub_arr.item() returns a sized array

hpaulj

len of an 0d array produces this error:

In [77]: x = np.array(123)
In [78]: x
Out[78]: array(123)
In [79]: x.shape
Out[79]: ()
In [80]: len(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-80-a7f4a5366567> in <module>()
----> 1 len(x)

TypeError: len() of unsized object

loadmat uses object dtype arrays to represent MATLAB cells and struct.

What's the dtype of sub_arr? You may need to use sub_arr.item()orsub_arr[()]` to extract that item from the object layer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert an array stored as a string, to a proper numpy array

Average of a numpy array returns NaN

Numpy where returns empty array

Dereference void pointer to numpy array or list

How is the data of a numpy.array stored?

Check if Numpy Array is Stored in Shared Memory

Some confusions on how numpy array stored in Python

Replace entry in specific numpy array stored in dictionary

Is there a way to somehow extract an int[ ] array value from a method that returns a void?

function returns void even if i set the return statement to array?

Splitting numpy multidimensional array based on indices stored in another array or list

numpy search array for multiple values, and returns their indices

sitk.GetArrayFromImage returns empty numpy array?

numpy.vectorize of function that returns an array

Numpy error when converting array of ctypes types to void pointer

Batch operation to all matrices stored in a n-dimensional numpy array

Pandas: Getting back original numpy array stored in DataFrame

How to discretize stored data in numpy array using Orange?

Why won't OpenCV show an image stored in a numpy array? (Python)

Fastest way to transpose a matrix stored in an 1D array in NumPy?

Why is the array using numpy.reshape not being stored properly?

Class objects stored in numpy.array() not working properly

Extracting instance variable from object stored in numpy array

Numpy.mean returns different results for array and copy of array

Dividing integer with numpy.array returns array with zeros

Retrieving a string value stored in an array that's stored in a JSON returns only the first character of the string

Test method that returns void

QCombobox finData method always returns -1 with numpy array

After converting str to float, numpy array returns strings

TOP Ranking

HotTag

Archive