0

I need to add an element to the numpy array. But append and extend are both not working. Here a1 is a function.

import numpy as np
def a1(f):
    return 700*(np.exp(f/1125.0) - 1)

f = np.zeros(26)
N = 26
f_min = 300
f_max = 16000

for n in range(N):
    f[n] = (f_min + n*(f_max - f_min)/(N-1))

h = a1(f)
h = h[::-1]
np.append(h, 0)
print h

The output is:

[ 16000.          14221.33951611  12632.11814102  11212.15935635
  9943.43557641   8809.83927267   7796.97847492   6891.99405301
  6083.39645924   5360.91985872   4715.39179592   4138.6167425
  3623.27204809   3162.81497306   2751.39962281   2383.80272921
  2055.35733672   1761.89355146   1499.68560083   1265.40453166
  1056.07594631    869.04224018    701.92886113    552.61416253
  419.20246718    300.        ]

Why is the 0 in the last is not getting appended? Also the type of h is numpy.ndarray.

2
  • Instead of that for-loop (and np.zeros), you could use f = np.linspace(f_min, f_max, N). Commented May 26, 2015 at 12:03
  • I'll implement that. Thanks! Commented May 26, 2015 at 12:20

1 Answer 1

2

You need to assign np.append(h, 0) to h like

h = np.append(h, 0)
print h
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.