9

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!!

askedNov 28, 2016 at 4:32
Sterling Butters's user avatar
2
  • There are many ways to do this, what did you try so far?CommentedNov 28, 2016 at 4:39
  • List comprehensions are your friend.CommentedNov 28, 2016 at 4:41

7 Answers7

12

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...

answeredNov 28, 2016 at 4:46
mgilson's user avatar
Sign up to request clarification or add additional context in comments.

Comments

6

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')
answeredNov 28, 2016 at 8:54
Daniel F's user avatar

1 Comment

See Numpy documentation onnp.pad forreflect andsymmetric andwrap and numerous other very interesting and useful modes!
3
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]]
answeredNov 28, 2016 at 4:55
backtrack's user avatar

Comments

1

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)?

answeredNov 28, 2016 at 4:43
hpaulj's user avatar

Comments

1

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 again
answeredNov 28, 2016 at 4:47
kaveh's user avatar

Comments

1

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]])
answeredNov 28, 2016 at 4:49
Rufflewind's user avatar

Comments

0
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)))])]
answeredNov 28, 2016 at 5:07
Ben's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.