Javascript 从数组中检索列的有效方法是什么?
任何语言的示例都会有所帮助,不过最后我将使用Ruby或JavaScript/CoffeeScript 我有一个像素值数组。例如,我有一个表示5x5图像像素的数组Javascript 从数组中检索列的有效方法是什么?,javascript,ruby,arrays,Javascript,Ruby,Arrays,任何语言的示例都会有所帮助,不过最后我将使用Ruby或JavaScript/CoffeeScript 我有一个像素值数组。例如,我有一个表示5x5图像像素的数组 image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5] 图像如下所示: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 我已经有了一个办法来解决问题。但我想找到一种方法来检索索引的列 假设我在Ruby中有一个方法: c
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
图像如下所示:
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
我已经有了一个办法来解决问题。但我想找到一种方法来检索索引的列
假设我在Ruby中有一个方法:
class Array
def column(index)
...
end
end
我希望得到以下结果:
image.column(0) #=> [1,6,1,6,1]
image.column(3) #=> [4,9,4,9,4]
编辑:感谢所有提供帮助的人。以下是我的观点:
def column index
output = []
i = 0
while i < @height
output << pixel(index + (i * @width))
i += 1
end
output
end
def列索引
输出=[]
i=0
而我
输出许多其他答案适用于精确为5x5的图像,正如您的问题所指定的,但如果这不是隐式正确的,我将为此构建一个类,例如:
class ImageMap
attr_reader :image
def initialize(image,columns=nil)
@image = image.each_slice(columns ||= Math.sqrt(image.size)).to_a
end
def columns
@image.first.size
end
def rows
@image.size
end
def column(n)
@image.map{|a| a[n]}
end
def row(n)
[@image[n]].concat([nil] * columns).take(columns).flatten
end
def cell(column,row)
column(column)[row]
end
def print
@image.each {|a| puts a.join}
end
end
这将处理所有图像,并允许您设置预期列数。如果没有列期望值,那么它将尝试使其成为正方形
方形
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
im = ImageMap.new(image)
im.column(0)
#=> [1, 6, 1, 6, 1]
im.column(3)
#=> [4, 9, 4, 9, 4]
im.row(0)
#=> [1, 2, 3, 4, 5]
im.cell(4,2)
#=> 5
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
im = ImageMap.new(image,4)
im.column(0)
#=> [1, 5, 9, 3, 7, 1, 5]
im.columns
#=> 4
im.rows
#=> 7
class Array2D
def initialize(arr, ncols)
raise ArgumentError, "ncols must be positive" if ncols < 1
raise ArgumentError,
"arr.size must a multiple of ncols" unless (arr.size % ncols).zero?
@arr = arr
@ncols = ncols
@nrows = arr.size/ncols
end
def [](r,c) @arr[r*@ncols+c] end
def row(r) @arr[r*@ncols, @ncols] end
def rows_at(*indices) indices.map { |i| row(i) } end
def col(c) @nrows.times.map { |r,a| self[r,c] } end
def cols_at(*indices) indices.map { |i,a| col(i) } end
def array() rows_at(*0...@nrows) end
def transpose() cols_at(*0..@ncols) end
alias :to_s :array
end
Array2D.instance_methods(false)
#=> [:[], :row, :rows_at, :col, :cols_at, :array, :transpose, :to_s]
image = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,10,21,22,23,24,25]
image2D = Array2D.new(image, 5)
image2D.array
#=> [[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 0],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 10],
# [21, 22, 23, 24, 25]]
image2D[1,3]
#=> 9
image2D.row(1)
#=> [6, 7, 8, 9, 0]
image2D.rows_at(1,3)
#=> [[6, 7, 8, 9, 0], [16, 17, 18, 19, 10]]
image2D.col(1)
#=> [2, 7, 12, 17, 22]
image2D.cols_at(1,3)
#=> [[2, 7, 12, 17, 22],
# [4, 9, 14, 19, 24]]
image2D.transpose
#=> [[1, 6, 11, 16, 21],
# [2, 7, 12, 17, 22],
# [3, 8, 13, 18, 23],
# [4, 9, 14, 19, 24],
# [5, 0, 15, 10, 25]]
非方形
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
im = ImageMap.new(image)
im.column(0)
#=> [1, 6, 1, 6, 1]
im.column(3)
#=> [4, 9, 4, 9, 4]
im.row(0)
#=> [1, 2, 3, 4, 5]
im.cell(4,2)
#=> 5
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
im = ImageMap.new(image,4)
im.column(0)
#=> [1, 5, 9, 3, 7, 1, 5]
im.columns
#=> 4
im.rows
#=> 7
class Array2D
def initialize(arr, ncols)
raise ArgumentError, "ncols must be positive" if ncols < 1
raise ArgumentError,
"arr.size must a multiple of ncols" unless (arr.size % ncols).zero?
@arr = arr
@ncols = ncols
@nrows = arr.size/ncols
end
def [](r,c) @arr[r*@ncols+c] end
def row(r) @arr[r*@ncols, @ncols] end
def rows_at(*indices) indices.map { |i| row(i) } end
def col(c) @nrows.times.map { |r,a| self[r,c] } end
def cols_at(*indices) indices.map { |i,a| col(i) } end
def array() rows_at(*0...@nrows) end
def transpose() cols_at(*0..@ncols) end
alias :to_s :array
end
Array2D.instance_methods(false)
#=> [:[], :row, :rows_at, :col, :cols_at, :array, :transpose, :to_s]
image = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,10,21,22,23,24,25]
image2D = Array2D.new(image, 5)
image2D.array
#=> [[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 0],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 10],
# [21, 22, 23, 24, 25]]
image2D[1,3]
#=> 9
image2D.row(1)
#=> [6, 7, 8, 9, 0]
image2D.rows_at(1,3)
#=> [[6, 7, 8, 9, 0], [16, 17, 18, 19, 10]]
image2D.col(1)
#=> [2, 7, 12, 17, 22]
image2D.cols_at(1,3)
#=> [[2, 7, 12, 17, 22],
# [4, 9, 14, 19, 24]]
image2D.transpose
#=> [[1, 6, 11, 16, 21],
# [2, 7, 12, 17, 22],
# [3, 8, 13, 18, 23],
# [4, 9, 14, 19, 24],
# [5, 0, 15, 10, 25]]
显然,这可能需要对越界值进行一些处理,但您应该能够处理这个问题。不存在行/列的示例:
im.column(7)
#=> [nil, nil, nil, nil, nil]
im.row(7)
#=> [nil, nil, nil, nil, nil]
im.cell(7,2)
#=> nil
还要注意,如果它不是正方形,它仍将起作用,例如
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,4] # added a 4
im = ImageMap.new(image)
im.column(0)
#=> [1, 6, 1, 6, 1, 4]
im.column(1)
#=> [2, 7, 2, 7, 2, nil]
im.image
#=> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [1, 2, 3, 4, 5], [4]]
基于OP当前解决方案的更新
这种方法应该执行相同的功能,并且更鲁比
def column index
(0...@height).map { |i| pixel(index + (i * @width)) }
end
<>而不是在类<代码>数组中创建新的方法,您可以考虑创建一个单独的类。我假设image
的大小是多行的倍数——考虑到这个例子,我们讨论的是像素——但是如果放弃这个假设,代码显然可能会改变
感谢@SergioTulentsev和@engineersmnky的建议(见评论),我已经实施了这些建议
代码
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
im = ImageMap.new(image)
im.column(0)
#=> [1, 6, 1, 6, 1]
im.column(3)
#=> [4, 9, 4, 9, 4]
im.row(0)
#=> [1, 2, 3, 4, 5]
im.cell(4,2)
#=> 5
image = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
im = ImageMap.new(image,4)
im.column(0)
#=> [1, 5, 9, 3, 7, 1, 5]
im.columns
#=> 4
im.rows
#=> 7
class Array2D
def initialize(arr, ncols)
raise ArgumentError, "ncols must be positive" if ncols < 1
raise ArgumentError,
"arr.size must a multiple of ncols" unless (arr.size % ncols).zero?
@arr = arr
@ncols = ncols
@nrows = arr.size/ncols
end
def [](r,c) @arr[r*@ncols+c] end
def row(r) @arr[r*@ncols, @ncols] end
def rows_at(*indices) indices.map { |i| row(i) } end
def col(c) @nrows.times.map { |r,a| self[r,c] } end
def cols_at(*indices) indices.map { |i,a| col(i) } end
def array() rows_at(*0...@nrows) end
def transpose() cols_at(*0..@ncols) end
alias :to_s :array
end
Array2D.instance_methods(false)
#=> [:[], :row, :rows_at, :col, :cols_at, :array, :transpose, :to_s]
image = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,10,21,22,23,24,25]
image2D = Array2D.new(image, 5)
image2D.array
#=> [[ 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 0],
# [11, 12, 13, 14, 15],
# [16, 17, 18, 19, 10],
# [21, 22, 23, 24, 25]]
image2D[1,3]
#=> 9
image2D.row(1)
#=> [6, 7, 8, 9, 0]
image2D.rows_at(1,3)
#=> [[6, 7, 8, 9, 0], [16, 17, 18, 19, 10]]
image2D.col(1)
#=> [2, 7, 12, 17, 22]
image2D.cols_at(1,3)
#=> [[2, 7, 12, 17, 22],
# [4, 9, 14, 19, 24]]
image2D.transpose
#=> [[1, 6, 11, 16, 21],
# [2, 7, 12, 17, 22],
# [3, 8, 13, 18, 23],
# [4, 9, 14, 19, 24],
# [5, 0, 15, 10, 25]]
最好在StackOverflow上询问实现问题。这是一个实现问题,所以应该转到so。您的问题的答案将(应该)主要基于代码。即,获取每行的大小,并使用该大小增加索引值以检索列。因此,如果您想要第三列的值,您将获取myArray[2+行大小*0]、myArray[2+行大小*1]、myArray[2+行大小*2]等。非数学方法是从行大小迭代到长度步进行大小,每次收集当前值。我明白您的观点。感谢您将问题迁移到SO。另外,我意识到这个问题是非常基本的。如果你这样使用image
,那么在Array
类上定义方法有什么意义呢?如果5
是一个参数,那么该方法会更健壮。OP明确提到他们希望在Array
类上定义该方法。@sawa OP简单地说了一下。。。。我读这篇文章是因为我可以用这种方式实施,但我愿意接受建议。所以我倾向于同意Sergio的观点,它不必在Array类中,事实上我甚至不会那样实现它。只是举个例子。列大小等于行大小是您自己的假设,这可能不是真的。@sawa修复了my类以允许自定义列大小。行将由列决定。另外,在不均匀或不存在的情况下,固定行是正确的行。非常详细的回答。谢谢大家!@拉文斯顿谢谢你。我还用一个方法更新了我的答案,该方法将返回与您的解决方案相同的结果,但更为惯用的是ruby。这肯定是非常糟糕的,因为您正在重新定义父API([]
),因此违反了LSP。还有其他罪,但这一个就够了:)伊加德,@Sergio,我在想什么??我删除了
,并承诺不再这样做。(我想我的脑子里有数组。)呵呵。一如既往,这是一个很好的解决方案。虽然每个带有_object([])
的_使用map
似乎会更简洁,例如def rows_at(*index)index.map{i | row(i)}end
@engineersmnky,谢谢。在这种情况下,我似乎经常忘记map
。在阅读了你的答案后,我还将参数nrows
更改为ncols
,这似乎更合理。