0

this is my MATLAB code with the following output:

pad=nan(1,5)

pad =

   NaN   NaN   NaN   NaN   NaN

I want to do the same operation in python, I tried np.isnan(1,6) but this is not working. what should I used to get the same results. thank you

sj95126
  • 6,520
  • 2
  • 15
  • 34

2 Answers2

2

You can use np.full() to create a multi-dimensional array pre-populated with the same values:

np.full((1, 5), np.nan)

which produces:

array([[nan, nan, nan, nan, nan]])
sj95126
  • 6,520
  • 2
  • 15
  • 34
  • 1
    why not np.full(5, np.nan) ? this give us array([nan, nan, nan, nan, nan]) –  Oct 09 '22 at 13:42
  • 1
    @user20191650: Because the two-argument form of Matlab's `nan()` returns a multi-dimensional array. – sj95126 Oct 09 '22 at 13:43
0

You can use numpy.zeros(N) + numpy.nan, where N is the number of NaN you want in your array.

import numpy as np

N = 6
nan_array = np.zeros(N) + np.nan

Will produce the following array -

[array([nan, nan, nan, nan, nan, nan])]

Faruk Ahmad
  • 128
  • 10