2

I am new in python and I have a question about array/matrix. Below is the matrix I got.

A =

[[85 77 83 ..., 59 58 59]

[80 83 80 ..., 57 60 58]

[75 76 81 ..., 59 58 60]]

I want to re-sample(I don't know if this is the right word) the matrix so it becomes

B =

[[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]]

I searched online and looked at many posts, but still I have no clue how to do this. So please teach me how to do this, and I am greatly appreciated.

2
  • I think if you explained what exactly you want to do to every row/element in the matrix you will find out that you have actually described the algorithm to do so and can implement it yourself in Python. Commented Apr 26, 2011 at 4:50
  • This will help: stackoverflow.com/questions/5586719/… Commented Apr 26, 2011 at 5:04

4 Answers 4

3

Definitely use the info from Scipy interpolation how to resize/resample 3x3 matrix to 5x5? from the comments.

But I thought I'd mess around and here's what I got:

Possibly the worst looking method of all time:

>>> import pprint
>>> a = [[85, 77, 99],
...      [11, 22, 33],
...      [44, 55, 66]]
>>> 
>>> def transform(n,matrix):
...     return [item for sublist in [[[item for sublist in [[element]*n for element in row] for item in sublist] for _ in range(n)] for row in matrix] for item in sublist]
... 
>>> pprint.pprint(transform(3,a))
[[85, 85, 85, 77, 77, 77, 99, 99, 99],
 [85, 85, 85, 77, 77, 77, 99, 99, 99],
 [85, 85, 85, 77, 77, 77, 99, 99, 99],
 [11, 11, 11, 22, 22, 22, 33, 33, 33],
 [11, 11, 11, 22, 22, 22, 33, 33, 33],
 [11, 11, 11, 22, 22, 22, 33, 33, 33],
 [44, 44, 44, 55, 55, 55, 66, 66, 66],
 [44, 44, 44, 55, 55, 55, 66, 66, 66],
 [44, 44, 44, 55, 55, 55, 66, 66, 66]]
>>> pprint.pprint(transform(4,a))
[[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66]]
>>> pprint.pprint(transform(5,a))
[[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66]]
>>> 
Sign up to request clarification or add additional context in comments.

Comments

2

The following implementation recursively resamples (duplicates) a matrix or a list (any iterable container type) containing numbers (any non-iterable objects). It is fast and much simpler to comprehend than other alternatives. It can handle arbitrarily-nested lists. Every sublist is properly deep-copied.

import itertools

def resample(obj, n):
    try:
        return list(itertools.chain.from_iterable((resample(row, n) for c in xrange(n)) for row in obj))
    except TypeError:
        return obj

Usage:

>>> l = [1, 2, 3, 4]
>>> resample(l, 4)
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]

>>> m = [[1, 2, 3, 4], [5, 6, 7, 8]]
>>> resample(m, 4)
[[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
 [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
 [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
 [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
 [5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
 [5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
 [5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
 [5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8]]

Comments

0

I didn't exactly understand your algorithm or what you want to do, but:

a=[[1,2],[3,4]]
# grow horizontally, 5 times
b=[[c for d in zip(x,x,x,x,x) for c in d] for x in a]
# grow vertically, 5 times
c= [z[:] for x in ((y[:],y[:],y[:],y[:],y[:]) for y in b) for z in x]

Note that it works with arrays of anything as it uses only the base language primitives

Comments

0
import numpy as np
import scipy as sp

# your matrix. Let's say A(3,3) with random values from 0 to 20
A = sp.random.randint(20,size=(3,3))

# Resize as you want (m x n)
m =5
n =5
New_A = sp.kron(A, sp.ones(m,n))

print New_A

Even it is a bit late for the answer I preferred to give some comments on it!

Comments

Your Answer

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