سؤال

I'm trying to make a 6x6 matrix in NumPy:

import numpy as np
a = 0.01; c = 0.1; b = 1-c*a

A = np.matrix([1,0,0,a,0,0],[0,1,0,0,a,0],[0,0,1,0,0,a],[0,0,0,b,0,0],[0,0,0,0,b,0],[0,0,0,0,0,b])

It raises an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() takes at most 4 arguments (7 given)

I searched in the documentation and it doesn't mention a limitation for a 6x6 matrix, only for extremely large matrices (ditto for the questions I found on NumPy matrices here on the Stack Exchange.)

And other online answers suggest the variables are a-okay to put in.

I could try to fill my matrix using loops, but I know that is computationally more expensive. Alternatively, writing additional lines of code to manually replace each value I want to switch (from ones or zeros for example, one of the standard matrix functions) would be quite a lot of code writing to make my matrix...and I have a sneaking suspicion that there's an easier way.

1) Can someone explain what the problem is here? and 2) What is a fast and simple way to make a 6x6 matrix (that's nonstandard, so filled with odd values or variables that are predefined above) in NumPy?

Thanks!

هل كانت مفيدة؟

المحلول

You forgot the outer brackets.

A = np.matrix([[1,0,0,a,0,0],[0,1,0,0,a,0],[0,0,1,0,0,a],[0,0,0,b,0,0],[0,0,0,0,b,0],[0,0,0,0,0,b]])

The numpy.matrix constructor takes a single array-like argument to initialize the matrix, not a sequence of positional arguments representing the rows. (Additional positional arguments set things like the dtype.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top