【发布时间】:2011-11-03 11:06:07
【问题描述】:
我有一个名为 terin.dat 的文件,其中包含这个矩阵:
10
1 1 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 12 12 12
1 2 3 4 5 6 7 12 12 12
1 2 3 4 5 6 7 12 12 12
我想读入文件并使用第一行的第一个数字作为矩阵的大小(在本例中为 10 X 10)。然后用下面的数字填充 10 X 10 矩阵。
这是我目前所拥有的:
class Terrain
def initialize file_name
@input = IO.readlines(file_name) #read in file # reads in the file with the terrain detials
@matrix_size = @input[0].to_i # changes the first index to an int (so i can make a10X10 matrix)
@land = Matrix.[@matrix_size, @matrix_size] # will this make a 10 X 10 matrix??
end
end
我想知道这是否会生成一个 10X10 的矩阵以及如何填充它??
【问题讨论】: