ValueError: could not broadcast input array from shape (4,1) into shape (4)

0 votes
This code produces this error of broadcasting and I don't understand why. N is a number, C is a matrix 4x4, xvec_tra an array 4x1 and the result x should be a matrix 4xN.

x=np.zeros(shape=(4,int(N)), dtype=np.float64)
for i in range(0, int(N/2)):
    if i == 0:   
        x[:,0]=C * xvec_tra
        print(x[:,0])
else:
    x[:,i]=C*x[:,i-1]
    print(x[:,i])
Feb 13 in Python by Giorgio
• 120 points
709 views

1 answer to this question.

0 votes

Hey @Giorgio,

You can try this hope this will resolve your error.

import numpy as np

N=0

C=0

x=np.zeros(shape=(4,int(N)), dtype=np.float64)

for i in range(0, int(N/2)):

    if i == 0:   

        x[:,0]=C * xvec_tra

        print(x[:,0])

    else:

      x[:,i]=C*x[:,i-1]

      print(x[:,i])

print(N)
answered Feb 18 by Gitika
• 29,170 points

Related Questions In Python

0 votes
1 answer
+1 vote
0 answers

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

I was implementing gillespie algorithm and when ...READ MORE

Dec 23, 2019 in Python by anonymous
• 130 points

reshown Jan 13 by Kalgi 1,069 views
0 votes
1 answer
+1 vote
0 answers

ValueError: operands could not be broadcast together with shapes (3,) (1000,)

This is the part of my code, why ...READ MORE

May 17 in Python by anonymous
• 200 points

edited May 18 by Gitika 130 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 935 views
0 votes
0 answers
+4 votes
6 answers
0 votes
1 answer
0 votes
1 answer

How can we remove an element from dic type variables. Eg : a={'A' : 2 , 'K' : 4 , 'D' : 6} }

Hey, @Kanishka, Suppose your dict contains: a={'A' : 2 ...READ MORE

answered May 20 in Python by Gitika
• 29,170 points
85 views