0

I have a vector y of size 4 x 1 , and another vector y2 of size 1 x 4, I need to concatenate the vectors y and real and imaginary parts of y2.

The problem is that when I reshape the vector y2 into vector 4 x 1, and then concatenate it with vector y, it gives me an error of 'all the input array dimensions except for the concatenation axis must match exactly' .

Here is the code I made, So Y3 is expected to be a vector of size 12 x 1, but the last command gives an error:

import numpy as np

h = np.random.randn(4, 4) + 1j * np.random.randn(4, 4)
x = np.array([[1 + 1j], [0 + 0j], [0 + 0j], [0 + 0j]])
y = h.dot(x)
n = 3
y2 = np.zeros((1, 4), dtype=np.complex)
for ii in range(n):
    y2[: , ii] = np.linalg.pinv(h[: , ii].reshape(-1,1)).dot(x)
y_con = np.concatenate((np.real(y2),np.imag(y2)))
y_m = np.absolute(y)
y_con2 = y_con.reshape(8,1)
Y3 = np.concatenate((y_con2, y_m))
trotta
  • 1,205
  • 1
  • 13
  • 21
Gze
  • 388
  • 1
  • 10
  • This code runs. `y_con2` is (8,1), `y_m` is (4,1). `Y3` is (12,1), concatenated on 0 axis. `y_con` is (2,4), the result of concatenating two (1,4) arrays. – hpaulj Aug 03 '19 at 15:36
  • @hpaulj , I don't know why does it give me an error of Y3 = np.concatenate([y_con.reshape(-1,1), y_m]) ValueError: all the input arrays must have same number of dimensions – Gze Aug 03 '19 at 15:38
  • If you used `axis=1` when making `y_con`, the result would be (1,8). Or you could have started with `y2=np.zeros((4,1)...)`. – hpaulj Aug 03 '19 at 15:40
  • I ran this in an interactive Ipython session, so was able to look at the shape of all the variables. And play around with alternatives. – hpaulj Aug 03 '19 at 15:42

1 Answers1

1
Y3  = np.concatenate([y_con.reshape(-1,1), y_m])

y_m is 4 X 1 so reshape y_con to n X 1 to concatenate row wise

mujjiga
  • 12,887
  • 2
  • 22
  • 39
  • You mean no need to run this line ? y_con2 = y_con.reshape(8,1) ?? – Gze Aug 03 '19 at 14:08
  • When I did this, I got a vector of shape (12,1) , however I need a vector of shape (12,) . I don't know what's the difference between them but I think there are difference between them !! – Gze Aug 03 '19 at 16:30
  • Just do reshape(-1) to change (12,1) to (12,). [about the difference](https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r) (`Y3 = Y3.reshape(-1)`) – mujjiga Aug 03 '19 at 17:41