Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an array whose elements are all equal to a specified value?

Tags:

How do I create an array where every entry is the same value? I know numpy.ones() and numpy.zeros() do this for 1's and 0's, but what about -1?

For example:

>>import numpy as np >>np.zeros((3,3)) array([[ 0.,  0.,  0.],        [ 0.,  0.,  0.],        [ 0.,  0.,  0.]])  >>np.ones((2,5)) array([[ 1.,  1.,  1.,  1.,  1.],        [ 1.,  1.,  1.,  1.,  1.]])  >>np.negative_ones((2,5)) ??? 
like image 290
maxm Avatar asked Jan 01 '13 16:01

maxm


People also ask

Is is possible to make all elements in the array equal?

Explanation: There is no way you can make all elements equal.

How do you initialize an array with all elements?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.

How do you check if all values in an array are equal?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.


1 Answers

Use np.full() as follows:

np.full((2, 5), -1.) 

Returns:

array([[-1., -1., -1., -1., -1.],        [-1., -1., -1., -1., -1.]]) 
like image 196
DontDivideByZero Avatar answered Sep 24 '22 14:09

DontDivideByZero