简体   繁体   中英

List indices must be integers, not tuple?

I have been given a .mat file which is 1024*1024*360 ie, a 3D object. I have divided the data in to three .mat files A,B and C. All three of them are 1024*1024*120 . I am loading them to a matrix 'mat' which is 1024*360 . I am loading each one of them one by one and then deleting them to make space. Basically it's just a 2D slice of the 3D object at the point 240. Later I am trying to plot the image. Following is my code :

import scipy.io 

import numpy as np
mat = np.zeros((1024,360))

x = scipy.io.loadmat('/home/imaging/Desktop/PRAKRITI/Project/A.mat')  
x = x.values()           
mat[:,0:120]= x[240,:,:]
del x


y = scipy.io.loadmat('/home/imaging/Desktop/PRAKRITI/Project/B.mat')
y = y.values()
mat[:,120:240]= y[240,:,:]
del y


z = scipy.io.loadmat('/home/imaging/Desktop/PRAKRITI/Project/C.mat')
z = z.values()
mat[:,240:360]= z[240,:,:]
del z


import matplotlib.py as plt
imageplot = plt.imshow(matrix)

I am getting this error :

mat[:,0:120]= x[240,:,:]
TypeError: List indices must be integers, not tuple

Can anyone suggest what I am doing wrong here?

You have to create a numpy array from the original x matrix. This is why the normal python array doesn't accept the numpy type fancy indexing, like matrix[x,y,z] only like matrix[x][y][z].

import scipy.io 

import numpy as np
mat = np.zeros((1024,360))

x = scipy.io.loadmat('/home/imaging/Desktop/PRAKRITI/Project/A.mat')  
x = np.array((x.values()))
mat[:,0:120]= x[240,:,:]
del x


y = scipy.io.loadmat('/home/imaging/Desktop/PRAKRITI/Project/B.mat')
y = np.array((y.values()))
mat[:,120:240]= y[240,:,:]
del y


z = scipy.io.loadmat('/home/imaging/Desktop/PRAKRITI/Project/C.mat')
z = np.array((z.values()))
mat[:,240:360]= z[240,:,:]
del z


import matplotlib.py as plt
imageplot = plt.imshow(matrix)

Alternately you can use x[240][:][:] instead of x[240,:,:]

Glad to have been of help! Feel free to accept my answer if you feel it was useful to you. :-)

continuing:

Because the following code worked fine, i guess the problem is somewhere at the loaded matrixs' dimensions iexvalues() etc. So please check it first, with print x.shape().

import numpy as np

mat = np.zeros((1024,360))
x = np.zeros((1024,1024,120))

mat[:,0:120] = x[240,:,:]

print mat

[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM