mat=[[0,1,5],[1,3,6],[-1,4,4],[1,2,2],[7,3,7],[2,5,3]]
mat matrix shape could be a 10000*5. here just an example
Here I define a function. It tries to find mat[:,0] < be or mat[:,0] > ba or mat[:,1] < bb. If one column match the condition, the element[i,0] < be the element[i,0]=be, then copy the column to another matrix "swape". Also delete this column from matrix "mat". Same as mat[:,0] > ba or mat[:,1] < bb. For mat[:,1] < bb, the colume will copy to "swapt",mat[:,0] > ba don't copy, just delete.
example be=0, ba=6,bb=3
The return list should be :
mat=[[1,3,6],[2,5,3]]
swape=[[-1,4,4]]
swapt=[[1,2,2],[0,1,5]]
The function will return mat, swape, and swapt
def swapco(be,ba,bb,mat):
swape=np.array()
swapt=np.array()
leng=np.shape(mat)[0]
for i in range(leng):
if mat[i,2]<bb:
mat[i,2]=bb
np.append(swapt,i,1)
np.delete(mat, i, 0)
else:
if mat[i,0]>=ba:
mat[i,0]=ba
np.append(swape,i,1)
np.delete(mat, i, 0)
elif mat[i,0]<=be:
mat[i,0]=be
np.append(swape,i,1)
np.delete(mat, i, 0)
i+=1
return swape, swapt
In my code, I found the matrix mat length always reduce once some columns matched the condition. It will append and delete wrong column. Also the append is append a address or a deepcopy?
If using
for col in mat:
Then how to delete itself in mat? Or any efficiency way to write this code?
Question updated...