-3

image

Hello, i am new to Python, and i need to create a very special matrix (see above). It just repeats 7 different values per row followed by zeros to the end of the row. After every row two zeros are filled and the array is repeated. When the array reaches the end, it will continue from the start until h0(2) is at index [x,0]. After that another h starts in the same way I think the naive way is to use nested and loops with counters and breaks.

In this post a similiar question has already been asked: Creating a special matrix in numpy but its not exactly what i need.

Is there a smarter way to create this instead of nested loops like in the previous post or is there even a function / name for this kind of matrix?

Community
  • 1
  • 1
  • Welcome to StackOverflow. Please include the code in minimal form into your question. External Links might go down and then your question wouldn't help any subsequent visitor. Please also read what correct questions should contain: [MCVE](http://stackoverflow.com/help/mcve) – MSeifert Feb 11 '16 at 23:27
  • I assume the second `h_0(0)` in the second row should be `h_0(1)`? –  Feb 12 '16 at 01:06
  • The ending criterion is a bit fuzzy (it seems to rely on the number of rows, not whether `h_x()` has completed "its loop"), but I'd just create a 1D list with a double for loop: the most practical and straightforward way. For efficiency, you could wrap that in list-comprehension. Once the list is there, turn it into a numpy array and reshape the thing to 3N/2 by N. –  Feb 12 '16 at 01:10
  • What you're describing looks like multiple stacked [Toeplitz matrices](https://en.wikipedia.org/wiki/Toeplitz_matrix). You could create the individual submatrices using [`scipy.linalg.toeplitz`](http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.toeplitz.html) with appropriately zero-padded *h* vectors, then row-wise concatenate them using `np.vstack`. – ali_m Feb 12 '16 at 03:45
  • Yes, Evert, you are right, the 2nd `h_0(0)` should be `h_0(1)`. – pythonFriend Feb 12 '16 at 14:01
  • I dont think that Toeplitz are useful here since diagonals are always different. I think the smartest way is like hpaulj describes to add `Hs` and then zeroes. The amount of zeros are always the same so i could calculate that depending on the matrix size. – pythonFriend Feb 12 '16 at 14:08

1 Answers1

0

I would focus on repeated patterns, and try to build the array from blocks.

For example I see 3 sets of rows, with h_0, h_1 and h_2 elements.

Within each of those I see a Hs = [h(0)...h(6)] sequence repeated.

It almost looks like you could concatenate [Hs, zeros(n), Hs, zeros(n),...] in one long 1d array, and reshape it into the (a,b) rows.

Or you could create a A = np.zeros((a,b)) array, and repeatedly insert Hs into the right places. Use A.flat[x:y]=Hs if Hs wraps around to the next line. In other words, even if A is 2d, you can insert Hs values as though it were 1d (which is true of its data buffer).

Your example is too complex to give you an exact answer in this short time - and my attention span isn't long enough to work out the details. But this might give you some ideas to work with. Look for repeated patterns and slices.

hpaulj
  • 221,503
  • 14
  • 230
  • 353