# Untitled

By: a guest on Aug 30th, 2012  |  syntax: None  |  size: 0.83 KB  |  hits: 11  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. import numpy as np
2.
3.
4. n = 4
5. data = np.reshape(range(4*4),(n,n))
6. data = data[:,:,np.newaxis]
7.
8. print data[:,:,0],data.shape
9.
10. seq = [0,1,2,3]
11.
12.
13. """seq
14. 0 : up/down
15. 1 : down/up
16. 2 : left/right
17. 3 : right/left
18. """
19.
20. def fold(data,seq):
21.
22.     if len(seq) == 0:
23.         print 'last=',data[0,0,:]
24.         return data
25.
26.     if seq[0]==0:
27.         n2 = data.shape[0]/2
28.         dfold = np.dstack((data[n2-1::-1,:],data[n2:,:]))
29.         fold(dfold,seq[1:])
30.
31.     if seq[0]==1:
32.         n2 = data.shape[0]/2
33.         dfold = np.dstack((data[:n2-1:-1,:],data[:n2,:]))
34.         fold(dfold,seq[1:])
35.
36.     if seq[0]==2:
37.         n2 = data.shape[1]/2
38.         dfold = np.dstack((data[:,n2-1::-1],data[:,n2:]))
39.         fold(dfold,seq[1:])
40.
41.     if seq[0]==3:
42.         n2 = data.shape[1]/2
43.         dfold = np.dstack((data[:,:n2-1:-1],data[:,:n2]))
44.         fold(dfold,seq[1:])
45.
46.
47. fold(data,seq)