How to determine a specific vector from a set of vectors in python

Question:

I have the following two-dimensional vectors Ui for i=1,…,1000

import numpy as np
n = 1000
u1 = np.random.uniform(0.5, 4.5, size=n)
u2 = 2 / u1 + np.random.normal(scale=0.09, size=n)
u = np.array([u1,u2])

How can I determine what the 500th two-dimensional vector or an arbitrary vector in Ui is?

Asked By: Mahtab

||

Answers:

It’s a little confusing to call these "two-dimensional vectors", because in NumPy, "dimension" refers to (the length of) the shape of an array. For example, a vector is a one-dimensional array, a matrix is a two-dimensional array, and higher-dimensional arrays are also possible. In particular, u is a two-dimensional array with shape (2, n).

IIUC, you want to get the 500th (say) column of u, which is a one-dimensional array (vector) with two elements. You can do that with u[:, 499].

More information can be found in the NumPy documentation on indexing.

Answered By: Arne
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.