1

I'm having an issue where I'm adding two 4x1 arrays and the result is a 4x4 array where the first column is repeated 4 times. The result I need is a 4x1 array.

I've initialized an array as such (m = 4): z = np.zeros((m, len(t))

Later in my code I pass this array into a function as z[:,k+1] so the dimensionality becomes a 4x1 array. (Note that when I print this array to my terminal is shows up as a row vector and not a column vector: [0. 0. 0. 0.], I'm not sure why this is either). The array that I'm trying to add to z has the following structure when printed to my terminal:

[[#] [#] [#] [#]]

Clearly the addition is pulling the above array into each element of z instead of adding their respective components together, but I'm not sure why as they should both be column vectors. I'd appreciate any help with this.

EDIT: I have a lot of code so I've included a condensed version that hopefully gets the idea accross.

n = 4   # Defines number of states
m = 4   # Defines number of measurements
x = np.zeros((n, len(t)), dtype=np.float64) # Initializes states
z = np.zeros((m, len(t)), dtype=np.float64) # Initializes measurements
u = np.zeros((1, len(t)), dtype=np.float64) # Initializes input
...
C = np.eye(m) # Defines measurement matrix
...
for k in range(len(t)-1):
  ...
  x_ukf[:,k+1], P_ukf[k+1,:,:] = function_call(x_ukf[:,k], z[:,k+1], u[:,k], P_ukf[k,:,:], C, Q, R, T) # Calls UKF function

This then leads to the function where the following occurrs (note that measurement_matrix = C (4x4 matrix), X is a 4x9 matrix, and W a 1x9 row vector):

Z = measurement_matrix @ X  # Calculates measurements based on sigma points
zhat = Z @ W.T
...
state_vec = state_vec + K @ (measurement_vec - zhat) # Updates state estimates

The issue I'm having is with the expression (measurement_vec - zhat). This is where the result should be a 4x1 vector but I'm getting a 4x4 matric.

5
  • 2
    Hello, please provide the lines of code that may be a problem in your case. Commented May 29, 2020 at 18:54
  • @Sebastian I included my code in an edit. If you need more information let me know. I tried to follow the path of the z variable. Commented May 29, 2020 at 19:13
  • There's some minor confusion in terminology here: in NumPy, adding two "1D" arrays never gives a 2D array. One of the arrays you're adding is actually a "2D" array where one of the dimensions has length 1. This distinction is important in NumPy but can be confusing to users coming from Matlab. You can get rid of the dimensions of length 1 either by using np.squeeze or by selecting the index 0 from them. Commented May 29, 2020 at 19:43
  • @macroeconomist Hey, thanks for clearing that up. I tried indexing with as zhat[:,0] and it seemed to work, but I had to do the same for state_vec[:,0]. I thought I had wrapped my head around NumPy arrays but I'm still learning more and more about them. Commented May 29, 2020 at 20:08
  • In general keeping track of "row" and "column" vectors (actually 2D arrays with singleton dimensions) isn't that useful in NumPy, since you can get the same behavior without them: if x is a 1D array, then x @ A treats it like a row vector and multiplies by the matrix A, and A @ x treats it like a column vector. So I'd try cutting off the row vs. column distinction at the source, e.g. by using W[0, :] instead of W.T in getting zhat, so that zhat is one-dimensional. Commented May 29, 2020 at 20:21

1 Answer 1

1

This is sometimes called broadcasting:

a, b = np.arange(4), np.arange(8,12)

c = a + b[:,None]

Output:

array([[ 8,  9, 10, 11],
       [ 9, 10, 11, 12],
       [10, 11, 12, 13],
       [11, 12, 13, 14]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.