rob788
rob788

Reputation: 23

Why can't I assign an array as column of another array

I have this numpy array

data = np.array([10.66252794 10.65999505 10.65745968 10.65492432 10.65239142 10.64985606
 10.64732069 10.64478533 10.64225243 10.63971707 10.6371817  10.6346488
 10.63211344 10.62957807 10.62704518 10.62450981 10.62197445 10.61944155
 10.61690619 10.61437082])

I want the values in data to be in the p-th column of the array result.

Just to clarify, I want to achieve the same as Matlab's result(:,p)

I tried

result[..., p] = data

but this gives me

ValueError: could not broadcast input array from shape (20) into shape ()

Isn't numpy's result[..., p] the same as Matlab's result(:,p)

I also tried what it's been suggested here Assigning to columns in NumPy?

But result[...,p] = data[..., 0] puts in result only the first value of data which is 10.66252794

Upvotes: 1

Views: 587

Answers (1)

Mercury
Mercury

Reputation: 4171

You're trying to assign a column to an apparently empty array. You can only assign data of shape (20,) to any column in result if result is an array with mxn rows and columns, such that the number of rows, m = 20. Like:

result = np.zeros((20,5))
result[:,0] = data #Assigning to column 0

Upvotes: 1

Related Questions