【问题标题】:Is there a Python argument similar to R's matrix(c())?是否有类似于 R 的矩阵(c())的 Python 参数?
【发布时间】:2021-04-25 01:04:16
【问题描述】:

我正在查看我在网上找到的 R 代码。这是链接:http://epirecip.es/epicookbook/chapters/kr08/3_4/r_desolve

我对这条线特别有疑问。

beta <- matrix(c(2.089, 2.089, 2.086, 2.037, 2.089, 9.336, 2.086, 2.037, 2.086, 2.086, 
                 2.086, 2.037, 2.037, 2.037, 2.037, 2.037), nrow=4, ncol=4) 

我知道在 R 中,c() 函数返回一个向量,matrix() 从给定的一组值创建一个矩阵。在这方面,python 是否有类似 matrix(c()) 参数的参数/命令?哪个可以做同样的事情?如果是这样,欢迎提供任何答案、提示或有用的链接。

【问题讨论】:

  • 你可能想使用numpy

标签: python r matrix vector


【解决方案1】:

这会产生你想要的输出吗?

import numpy as np


# Set of values in the form of a numpy array.
l = np.arrange(16)

# If it's not already a numpy array use this
matrix = np.asarray(l).reshape(4, 4)

# But in this case you can just do
matrix = np.reshape(4, 4)

print(matrix)

【讨论】:

  • [x for x in range(16)] 是编写list(range(16)) 的一种不必要的复杂方式,但这是不必要的,您可以直接使用np.array(range(16)),但即使这样 也是不必要的,您可以这样做np.arange(16)
  • 好吧,我现在不觉得很傻!啊哈哈,谢谢!我很感激这些信息!
  • @BeanBagTheCat 谢谢你们的洞察力
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 2015-09-03
  • 2015-11-17
相关资源
最近更新 更多