0

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)
6
  • Please provide a reproducible example, check out minimal reproducible example Commented Jan 22, 2018 at 8:43
  • @juanpa.arrivillaga Edited. Thanks for commenting. Commented Jan 22, 2018 at 9:01
  • You should be able to access it like mesh['dim'] and mesh['x']... there seems to be a needless wrapping of an object type array, i.e., and array of other arrays. so to really dig down, you need to index into that, so for example mesh['dim'][0] should return your actual array of dimensions, e.g. array([[7, 6]]) Commented Jan 22, 2018 at 9:08
  • @juanpa.arrivillaga Yes, I agree and I can see that there is unnecessary wrapping. But that is the way I am getting the imported data from the .mat file by using scipy.io.loadmat. Commented Jan 22, 2018 at 9:20
  • @juanpa.arrivillaga Sorry, I changed the example slightly. Actually, I am importing a structure called dataSRC having following content: dataSRC.mesh 1x1 struct; dataSRC.field 1x1 struct; dataSRC.pod 1x1 struct. ` The structure mesh has following content: dataSRC.mesh.dim [89,108]; dataSRC.mesh.x 9612x1 double; dataSRC.mesh.y 9612x1 double Commented Jan 22, 2018 at 9:24

1 Answer 1

1

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)

Disclaimer

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.