2

10x10 array

Suppose you are trying to create a synthetic image 10x10 represented by a matrix of values (numpy array). This image has three blocked sections. In the upper left block, columns 0-4 and rows 0-4, the value will be 1. the right block, columns 5-9 and rows 0-9, will be 0. the remaining area, columns 0-4 and row 5-9, will be 2.(see attached image)

What is the fastest way to create such an object? I understand that you could create an array of zeros and iteratively change the values in each column, but I also understand this is not an efficient method. I assume it involves simply generating the array using np.array, but I'm not quite sure of the syntax.

asked May 30, 2015 at 1:27

4 Answers 4

2

My first thought is to create an 'empty' array of 0, and then fill the blocks of 1 and 2. E.g.

In [145]: C = np.zeros((10,10), int)
In [146]: C[:4,:4]=1
In [147]: C[:4,5:9]=2
In [148]: C
Out[148]: 
array([[1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
 [1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
 [1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
 [1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

You could also make the blocks (with np.ones etc), and concatenate them. hstack and vstack are just alternative APIs for concatenate. But concatenate ends up using, in compiled code, this initialize and assign method. It's a good idea to be familiar with both methods.

answered May 30, 2015 at 3:27

1 Comment

Great. This is the sort of method I was looking for. Thanks for your help
1

What about,

import numpy as np
a = np.ones((5,5))
b = a*2.
c = np.zeros((10,5))
np.hstack((np.vstack((a,b)),c))
answered May 30, 2015 at 1:48

1 Comment

I had thought about doing this, but I wasn't sure if there were any even more compact way to accomplishing this in one go. Perhaps with np.arange?
0

Is this a homework question? Have a play with numpy.concatenate and numpy.ones and see how you go.

answered May 30, 2015 at 1:47

1 Comment

No, a visualization for an image segmentation program. I am just very new with python. thank you for your addition.
0

For simple patterns similar to yours, you can use basic broadcasting:

>>> numpy.array([1]*5 + [2]*5)[:,None] * numpy.array([1]*5 + [0]*5)
array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
 [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
 [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
 [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
 [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
 [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]])

[:,None] just adds a second axis, so that instead of a (10,) size array, we have a (10,1) size array which we can then multiply with the (10,) array on the right using broadcasting.

Or, more concisely:

>>> numpy.outer([1]*5 + [2]*5, [1]*5 + [0]*5)

which gives the same result.

answered Feb 23, 2019 at 1:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.