Slice an array

Asked

Viewed 63 times

0

I’m having trouble with x2 values when trying to slice to display in graph 2 does not appear anything and I can’t see where I am missing?

import matplotlib.pyplot as plt
import numpy as np

xold = np.random.rand()
N = 3000

x1 = np.empty((N))
print("x1:", x1)

for k in range(N):
    x_new = 4*xold*(1 - xold)
    xold = x_new
    x1[k] = x_new

comp = len(x1)
x2 = x1 + 0.25*np.std(x1)*np.random.rand(1,comp)

plt.figure(1)
plt.grid(True)

plt.subplot(2,1,1)
plt.plot(x1[:-1], x1[1:], 'bo')
plt.title('MAPA LOGISTICO 1')
plt.ylabel('x1k)')
plt.xlabel('x1(k-1)')

plt.subplot(2,1,2)
plt.plot(x2[:-1], x2[1:], 'bo')
plt.title('MAPA LOGISTICO 2')
plt.ylabel('x(2k)')
plt.xlabel('x2(k-1)')
plt.show()

1 answer

2


The problem is that the way you’re calculating, x2 turns into a two-dimensional matrix, with the outermost dimension having only 1 element.

Thus, x2[1:] and x2[:-1] do not return anything, because they are empty slices.

One way to solve is by extracting the internal vector, that is, before plotting, run:

x2 = x2[0]

The result:

plot

  • I didn’t quite understand your answer

  • 1

    @Ronaldosouza ok, however, the way I wrote, it seems clear enough to me, so it would be necessary for you to point out what you did not understand, so that we can edit and improve it.

  • I already understood what you said, and it solved. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.