访问向量数组麻烦 [英] Accessing an array of vectors trouble
本文介绍了访问向量数组麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下所示:
a = IO.readlines("input.txt").map { |line| Vector.[](line.split) }
现在我想访问我的 a 数组中第一个向量的单个组件.我正在编写以下内容来解决向量:
Now I want to access a single component of the first vector within my a array. I'm writing the following to address a vector:
puts a[0]
这种行为非常符合预期 - 我收到以下信息:
The behavior is pretty much expected - I receive the following:
Vector[1.2357, 2.1742, -5.4834, -2.0735]
现在让我们尝试以这种方式处理单个组件:
Now let's try to address a single component this way:
puts a[0][0]
瞧,我收到了所有矢量组件的列表,例如:
and voila, I receive a list of all the vector components,like:
1.2357
2.1742
-5.4834
-2.0735
怎么会?也许最后一次尝试是错误的?如何正确寻址数组中向量内的标量?
How come? Maybe the last attempt was wrong? How do I correctly address a scalar inside a vector within an array?
推荐答案
由于你的代码,我认为数组构造应该是:
Due to your code I think the array construction should be:
a = IO.readlines("input.txt").map { |line| Vector[*line.split] }
这篇关于访问向量数组麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文