包含cos和sin的python中的矩阵

时间:2022-05-09 19:58:08

I am a beginner in python. I have this matrix:

我是python的初学者。我有这个矩阵:

    Mat RX = (Mat_<double>(4, 4) <<
          1, 0,         0,          0,
          0, cos(roll), -sin(roll), 0,
          0, sin(roll), cos(roll),  0,
          0, 0,         0,          1);

How do I make this in python? It gives error when I try something like this:

我如何在python中做到这一点?当我尝试这样的事情时它会出错:

    MatrixX = np.mat('1,2,3;cos(roll),6,7')

2 个解决方案

#1


2  

Don't use the string constructor to np.mat. it's primarily there to ease matlab users in, and only works in the simplest cases. It also creates a np.matrix, which is generally not recommended to use.

不要将字符串构造函数用于np.mat。它主要用于缓解matlab用户,并且只能在最简单的情况下工作。它还会创建一个np.matrix,通常不建议使用它。

All you need here is:

这里你需要的是:

np.array([
    [1, 2, 3],
    [4, 5, np.cos(roll)]
])

(line-wrapping optional)

#2


0  

If you intepret cos(), and then construct the string, this can work:

如果你解释cos(),然后构造字符串,这可以工作:

Code:

MatrixX = np.mat('1,2,3;%s,6,7' % cos(roll))

To Test:

>>> np.mat('1,2;3,%s' % math.cos(2))
matrix([[ 1.        ,  2.        ],
        [ 3.        , -0.41614684]])

#1


2  

Don't use the string constructor to np.mat. it's primarily there to ease matlab users in, and only works in the simplest cases. It also creates a np.matrix, which is generally not recommended to use.

不要将字符串构造函数用于np.mat。它主要用于缓解matlab用户,并且只能在最简单的情况下工作。它还会创建一个np.matrix,通常不建议使用它。

All you need here is:

这里你需要的是:

np.array([
    [1, 2, 3],
    [4, 5, np.cos(roll)]
])

(line-wrapping optional)

#2


0  

If you intepret cos(), and then construct the string, this can work:

如果你解释cos(),然后构造字符串,这可以工作:

Code:

MatrixX = np.mat('1,2,3;%s,6,7' % cos(roll))

To Test:

>>> np.mat('1,2;3,%s' % math.cos(2))
matrix([[ 1.        ,  2.        ],
        [ 3.        , -0.41614684]])