ruby - Ruby 和类似类型语言中的数组是向量吗?

标签 ruby arrays r math vector

R 将 csv 数字列表(逗号分隔值,即 1, 2, 3)包装在 c() 函数中,该函数是 R 核心库的一部分,它将 csv 数字列表转换为向量。

这些向量看起来像 Ruby 或 Java 数组,不同之处在于这些 csv 列表包含在 c() 中,而不是 [] 中。看起来数组实际上是向量的子集。这是真的吗?如果是这样,这对于数组和矩阵意味着什么?

我发现关于该主题的一个讨论指出数组是静态向量。但在 Ruby 中,数组不是静态的。在 Ruby 中,数组是向量?

最佳答案

是的,在 R 中,数组只是一个带有给出数组维度的属性的向量。

来自?数组:

Details:

An array in R can have one, two or more dimensions. It is simply a vector which is stored with additional attributes giving the dimensions (attribute ‘"dim"’) and optionally names for those dimensions (attribute ‘"dimnames"’).

A two-dimensional array is the same thing as a ‘matrix’.

One-dimensional arrays often look like vectors, but may be handled differently by some functions: ‘str’ does distinguish them in recent versions of R.

The ‘"dim"’ attribute is an integer vector of length one or more containing non-negative values: the product of the values must match the length of the array.

也许您自己了解这一点的最简单方法是查看向量、矩阵和高维数组,如下所示:

a <- array(1:12, dim=c(2,2,3))
m <- matrix(1:4, ncol=2)
v <- c(1,2)

is(a)
# [1] "array"     "matrix"    "structure" "vector"    "vector"  
is(m)
# [1] "matrix"    "array"     "structure" "vector"
is(v)
# [1] "numeric" "vector" 

attributes(a)
# $dim
# [1] 2 2 3
attributes(m)
# $dim
# [1] 2 2
attributes(v)
# NULL

## Finally, try this
v <- 1:12
dim(v) <- c(2,2,3)
v

关于ruby - Ruby 和类似类型语言中的数组是向量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753419/

相关文章:

ruby - gem 安装错误 : timed out https://api. ruby​​gems.org/

javascript - 在数组的对象中找到未定义并插入对象

r - 有没有办法初始化或修改允许引用以前的值的列表?

regex - 替代 for() 循环以将非常大的数据框列条目与非常大的向量列表进行比较

r - 根据某些条件生成随机数

ruby-on-rails - rails/设计 : pass param after sign_in

mysql - 如何在Rails中仅从mysql的ActiveRecord集合中删除对象?

ruby - 帮助使用 Ruby 中的 "Whenever"gem 来执行 cron 任务

python - 如何在 scipy 中集成一个 numpy 数组?

c# - 使用 Newtonsoft json.net 在 C# 中反序列化 JSON 对象