1

I've to map R^2 array X=[x1, x2] into R^3 array X'=[x1, x2, x1^2].

Is there any compact solution?

ADDED: My starting array is a numpy array e.g. x = numpy.array([2, 3]). I want to end up with another numpy array e.g. x' = numpy.array([2, 3, 4]).

5
  • Could you give us an exemple of your X? Commented Jun 5, 2019 at 13:37
  • 2
    If X is a list, you can do [X[0], X[1], X[0]**2], or X + [X[0]**2] Commented Jun 5, 2019 at 13:39
  • Welcome to StackOverflow. Please make sure your questions are clear. In this case, the concept of "array" has multiple meanings in Python. It could mean a list of lists, or an array from the array module, or an ndarray from the numpy module. The last has some shortcuts to make array modifications quicker and easier and more compact. Which kind of array are you starting with and what kind of array do you want as a result? It would be best to show an example array, building it with Python code. Commented Jun 5, 2019 at 13:44
  • @RoryDaulton it sounds like they mean something to do with linear algebra arrays Commented Jun 5, 2019 at 13:58
  • Thank you, my staring array is a numpy array e.g. x = numpy.array([2, 3]), I want another numpy array e.g. x' = numpy.array([2, 3, 4]). Commented Jun 5, 2019 at 13:59

1 Answer 1

1

For the case where your array is a numpy array with shape (2,), one way to add the additional member is

xx = numpy.append(x, x[0]**2)

Unfortunately, if you try something like x + [x[0]**2] numpy will add the elements together rather than append the new element. If you try something like [x[0], x[1], x[0]**2] you end up with a list rather than a numpy array.

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.