2

I want to add a vector as the first column of my 2D array which looks like :

[[  1.     0.     0.      nan]
 [  4.     4.     9.97   1.  ]
 [  4.     4.    27.94   1.  ]
 [  2.     1.     4.17   1.  ]
 [  3.     2.    38.22   1.  ]
 [  4.     4.    31.83   1.  ]
 [  3.     4.    41.87   1.  ]
 [  2.     1.    18.33   1.  ]
 [  4.     4.    33.96   1.  ]
 [  2.     1.     5.65   1.  ]
 [  3.     3.    40.74   1.  ]
 [  2.     1.    10.04   1.  ]
 [  2.     2.    53.15   1.  ]]

I want to add an aray [] of 13 elements as the first column of the matrix. I tried with np.stack_column, np.append but it is for 1D vector or doesn't work because I can't chose axis=1 and only do np.append(peak_values, results)

2
  • This is an old post, but for anyone still looking, make numpy.c_ your friend. Essentially it handles any reshape and stacking operations necessary to combine arrays along their last axis. For the example above, if arr is the 2D array and v is the vector, all you need is np.c_[v, arr]. Note the square brackets. Commented Aug 27, 2021 at 18:47
  • Note that is equivalent to np.hstack((v.reshape(-1,1), arr)) but more powerful because it works in many other cases too. Commented Aug 27, 2021 at 18:53

4 Answers 4

1

I have a very simple option for you using numpy -

x = np.array( [[ 3.9427767, -4.297677 ],
 [ 3.9427767, -4.297677 ],
 [ 3.9427767, -4.297677 ],
 [ 3.9427767, -4.297677 ],
 [ 3.942777 , -4.297677 ],
 [ 3.9427767, -4.297677 ],
 [ 3.9427767, -4.297677 ],
 [ 3.9427767 ,-4.297677 ],
 [ 3.9427767, -4.297677 ],
 [ 3.9427772 ,-4.297677 ]])

b = np.arange(10).reshape(-1,1)
np.concatenate((b.T, x), axis=1)

Output-

array([[ 0.       ,  3.9427767, -4.297677 ],
       [ 1.       ,  3.9427767, -4.297677 ],
       [ 2.       ,  3.9427767, -4.297677 ],
       [ 3.       ,  3.9427767, -4.297677 ],
       [ 4.       ,  3.942777 , -4.297677 ],
       [ 5.       ,  3.9427767, -4.297677 ],
       [ 6.       ,  3.9427767, -4.297677 ],
       [ 7.       ,  3.9427767, -4.297677 ],
       [ 8.       ,  3.9427767, -4.297677 ],
       [ 9.       ,  3.9427772, -4.297677 ]])
Sign up to request clarification or add additional context in comments.

1 Comment

This raises ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1 and the array at index 1 has size 10. I think you meant (b, x) instead of (b.T, x).
1

Improving on this answer by removing the unnecessary transposition, you can indeed use reshape(-1, 1) to transform the 1d array you'd like to prepend along axis 1 to the 2d array to a 2d array with a single column. At this point, the arrays only differ in shape along the second axis and np.concatenate accepts the arguments:

>>> import numpy as np
>>> a = np.arange(12).reshape(3, 4)
>>> b = np.arange(3)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b
array([0, 1, 2])
>>> b.reshape(-1, 1) # preview the reshaping...
array([[0],
       [1],
       [2]])
>>> np.concatenate((b.reshape(-1, 1), a), axis=1)
array([[ 0,  0,  1,  2,  3],
       [ 1,  4,  5,  6,  7],
       [ 2,  8,  9, 10, 11]])

Comments

0

For the simplest answer, you probably don't even need numpy.

Try the following:

new_array = []
new_array.append(your_array)

That's it.

3 Comments

Afterward, you could simply transform this new_array into numpy format as you wish, by doing np.array(new_array).
I don't get where you add the vector. My 2D is array and 1D is vector, I have to do array.append(vector) ?
Nah don't worry about that. From the start, let's say your array name is my_2d_array. We do my_2d_list = [ ] and then my_2d_list.append(my_2d_array). Then you got a 2D list with your array as the first column. Finally, please do my_final_array = np.array(my_2d_list), which will transform the python list into numpy array format. Hope that helps!
-1

I would suggest using Numpy. It will allow you to easily do what you want.

Here is an example of squaring the entire set. you can use something like nums[0].

nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0] 
print even_squares  # Prints "[0, 4, 16]"

1 Comment

I don't understand how squaring a matrix is relevant to the question. The code shown isn't numpy either.

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.