I have a matrix of numbers:
[[a, b, c] [d, e, f] [g, h, i]]that I would like to be mirrored accordingly:
[[g, h, i] [d, e, f] [a, b, c] [d, e, f] [g, h, i]]And then again to yield:
[[i, h, g, h, i] [f, e, d, e, f] [c, b, a, b, c] [f, e, d, e, f] [i, h, g, h, i]]I would like to stick to basic Python packages like numpy. Thanks in advance for any help!!
7 Answers7
This can be accomplished using a simple helper function in pure python:
def mirror(seq): output = list(seq[::-1]) output.extend(seq[1:]) return outputinputs = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'],]print(mirror([mirror(sublist) for sublist in inputs]))Obviously, once the mirrored list is created, you can use it to create a numpy array or whatever...
Comments
Usenumpy.lib.pad with'reflect'
m = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]n=np.lib.pad(m,((2,0),(2,0)),'reflect')nOut[8]: array([['i', 'h', 'g', 'h', 'i'], ['f', 'e', 'd', 'e', 'f'], ['c', 'b', 'a', 'b', 'c'], ['f', 'e', 'd', 'e', 'f'], ['i', 'h', 'g', 'h', 'i']], dtype='<U1')1 Comment
reflect andsymmetric andwrap and numerous other very interesting and useful modes!import numpy as npX= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]A = np.asanyarray(X)B= np.flipud(A)C= np.concatenate((B, A[1:]), axis=0)D = C[:,1:]F = np.fliplr(C)E = np.concatenate((F, D), axis=1)print(E)I have added step by step transformation.flipud andflipud refrence
output
[[9 8 7 8 9] [6 5 4 5 6] [3 2 1 2 3] [6 5 4 5 6] [9 8 7 8 9]]Comments
This is taggednumpy so I'll assume your matrix is a 2d array
In [937]: A=np.arange(9).reshape(3,3)In [938]: AOut[938]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])flipping it on the rows:
In [939]: A[::-1,:]Out[939]: array([[6, 7, 8], [3, 4, 5], [0, 1, 2]])concatenating vertically
In [940]: np.concatenate((A[::-1,:],A), axis=0)Out[940]: array([[6, 7, 8], [3, 4, 5], [0, 1, 2], [0, 1, 2], [3, 4, 5], [6, 7, 8]])removing the duplicate first line
In [941]: np.concatenate((A[::-1,:],A[1:,:]), axis=0)Out[941]: array([[6, 7, 8], [3, 4, 5], [0, 1, 2], [3, 4, 5], [6, 7, 8]])Do you think you can do the same with a horizontal (column) reversal and concatenate (axis=1)?
Comments
And here is a non-numpy solution:
a = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]b = list(reversed(a[1:])) + a # vertical mirrorc = list(zip(*b)) # transposed = list(reversed(c[1:])) + c # another vertical mirrore = list(zip(*d)) # transpose againComments
Suppose you have
from numpy import array, concatenatem = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])You can invert this along the first (vertical) axis via
>>> m[::-1, ...]array([[7, 8, 9], [4, 5, 6], [1, 2, 3]])where::-1 selects the rows from last to first in steps of-1.
To omit the last row, explicitly ask for the selection to stop immediately before0:
>>> m[:0:-1, ...]array([[7, 8, 9], [4, 5, 6]])This can then be concatenated along the first axis
p = concatenate([m[:0:-1, ...], m], axis=0)to form:
>>> parray([[7, 8, 9], [4, 5, 6], [1, 2, 3], [4, 5, 6], [7, 8, 9]])This can be repeated along the other axis too:
q = concatenate([p[..., :0:-1], p], axis=1)yielding
>>> qarray([[9, 8, 7, 8, 9], [6, 5, 4, 5, 6], [3, 2, 1, 2, 3], [6, 5, 4, 5, 6], [9, 8, 7, 8, 9]])Comments
m = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]m_m = [[m[abs(i)][abs(j)] for j in range(-len(m)+1, len(m))] for i in range(-len(m)+1, len(m))]Or using numpy
m = array([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']])m_m = m.T[meshgrid(*2*[abs(arange(-len(m) + 1, len(m)))])]Comments
Explore related questions
See similar questions with these tags.



