这是用于从数组中检索一列的有效方法? [英] What is an efficient method for retrieving a column from an array?

查看:287
本文介绍了这是用于从数组中检索一列的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何语言中的一个例子会有所帮助,但最终我将使用Ruby或JavaScript的/ CoffeeScript的是。

An example in any language would help, though in the end I am going to be using either Ruby or JavaScript/CoffeeScript.

我有像素值的阵列。例如,我有一个重新presents一个5x5的图像的像素阵列。

I have an array of pixel values. For example, I have an array that represents the pixels of a 5x5 image.

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

我已经有越来越行的方式。但我想办法还检索索引中的列。

I already have a way of getting rows. But I want a way to also retrieve a column for an index.

比方说,我在Ruby中有一个方法:

Let's say I had a method in Ruby:

class Array
  def column(index)
    ...
  end
end

我想以下结果:

image.column(0) #=> [1,6,1,6,1]
image.column(3) #=> [4,9,4,9,4]

编辑:感谢大家谁提供的援助。下面是我用了:

Thanks to everyone who provided their assistance. Here is what I went with:

def column index
  output = []
  i = 0
  while i < @height
    output << pixel(index + (i * @width))
    i += 1
  end
  output
end

像素是另一种方法,我有一个返回从RGBA值的阵列的平均值,给定一个索引

Pixel is another method I have that returns an averaged value from an array of RGBA values, given an index.

这足够的原因是,我可以放心地假设,在图像中的每一行/列是正确的大小。我也想保持过程,我很可能使这个将使用输入钳位阵列(由于性能原因和帆布数据是Uint8ClampedArray事实)一个CoffeeScript的应用程序相当简单的,在这种情况下,输出值将是一个Uint8ClampedArray和我会使用索引,而不是推的,因为Uint8ClampedArray没有/不支持推送/流行/班/不印字。

The reason this suffices is that I can safely assume that every row/column in the image is the correct size. I also wanted to keep the process fairly simplistic as I am likely to make this a CoffeeScript application that will use typed clamped arrays(for performance reasons and the fact that canvas data is a Uint8ClampedArray), in which case the value of output would be a Uint8ClampedArray and I'd be using the index instead of push, since Uint8ClampedArray doesn't/can't support push/pop/shift/unshift.

推荐答案

很多其他的答案将为这正是5x5的你的问题指定但在的情况下图像地方工作,这不是隐真的,我将建立一个类为此,如:

Many of the other answers will work for an image that is exactly 5x5 as your question specifies but in the case where this is not implicitly true I would build a class for this such as:

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

这将处理所有的图像,并允许您还设置所需的列数为好。如果没有列的期望,就这样它试图使其平方米。

This will handle all images and allows you to also set the number of expected columns as well. If no column expectation is made then it tries to make it square.

广场

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

显然,这可以使用一些搬运,出界值,但你应该能够面对这一切。与不存在的行/列示例:

Obviously this could use some handling for out of bounds values but you should be able to deal with that. Example with non-existent rows/columns:

im.column(7)
#=> [nil, nil, nil, nil, nil]
im.row(7)
#=> [nil, nil, nil, nil, nil]
im.cell(7,2)
#=> nil

另外请注意,如果它不方将仍然起作用例如。

Also note if it is not square it will still function e.g.

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当前的解决方案更新
此方法应该preform相同的功能,是一个比较rubyesque

Update based on OP current solution This method should preform the same function and is a bit more rubyesque

def column index
  (0...@height).map { |i| pixel(index + (i * @width)) }
end

这篇关于这是用于从数组中检索一列的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆