0

I am using Python, trying to extract a matrix of distances from a .mat file. It comes in the form of a dictionary "dists" which is the following

print(dists)

enter image description here1

I want to get the matrix as an array: I tried extracting the value (Python allows it to be converted to an array) but I can't take only the array out, since it says it is 0-dimensional.

arr = np.array(dists[0].values())
print(type(arr))
print(arr)
print(arr[0])

enter image description here2

It looks like the array is 0-dimensional of the form ([[ ... ]]). How can I extract the 2D matrix?

I looked at this but couldn't figure out how to apply it to my problem. I tried arr[()] but it still thinks arr is a dictionary value rather than an array.

1 Answer 1

1

Firstly, you need to change dict_values() to list. I hope this example can help you.

import numpy as np
dishes = {'eggs': 2, 'sausage': 1, 'meat': 1, 'spam': np.ones((3,3))}
print(dishes.values())
arr = list(dishes.values())
print(arr)
print(arr[-1])

Terminal Output:

dict_values([2, 1, 1, array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])])
[2, 1, 1, array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
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.