I have an array A of just n elements. Making it (n x 1) .Is there a chance of making it (n x n) by putting all other elements as 'nan'. so that A.shape gives me (n,n) .
Asked
Active
Viewed 139 times
0
-
This question and its answers may be of assistance: [Initializing numpy matrix to something other than zero or one](http://stackoverflow.com/questions/1704823/initializing-numpy-matrix-to-something-other-than-zero-or-one) – Ryan Jul 08 '16 at 14:16
1 Answers
0
If there is any chance, that you could provide further information (such as what do you need it for), it would be great. Anyhow here is my solution:
import numpy as np
#input array
example = np.array([1,2,3,4,5])
#create a target matrix containing only 'nan' elements of size n*n
target = np.ones((len(example),len(example)))*float('nan')
#set first column to input array
target[0:,0] = example
result will be:
array([[ 1., nan, nan, nan, nan],
[ 2., nan, nan, nan, nan],
[ 3., nan, nan, nan, nan],
[ 4., nan, nan, nan, nan],
[ 5., nan, nan, nan, nan]])

Nikolas Rieble
- 2,416
- 20
- 43