Question: I have an array of arrays:

```
[
  [0,0,0,0],
  [0,1,0,0],
  [0,0,0,1],
  [0,0,0,0]
]

```

I want to find the index (row and column) of the items with value `1`. How can I do this? I need these values so that I can manipulate the cells at either side of the `1`s. I looked at `find.index` method, but I'm not sure.

Answer: 
Consider using the [Matrix](http://ruby-doc.org/stdlib-2.0.0/libdoc/matrix/rdoc/Matrix.html) class.

```
require 'matrix'

arr = [
  [0, 0, 0, 0],
  [0, 1, 0, 0],
  [0, 0, 0, 1],
  [0, 0, 0, 0]
]    

target = 1

Matrix[*arr].each_with_index.with_object([]) { |(e,row,col),a|
  a << [row,col] if e==target }
  #=> [[1, 1], [2, 3]]

```

I like how this reads.
```
z = [
  [0,0,0,0],
  [0,1,0,0],
  [0,0,0,1],
  [0,0,0,0]
]

```

First, let's find the matching rows:

```
rows = z.each_with_index.select { |row, index| row.include? 1}.map(&:last)
# => [1, 2]

```

And then for each row let's find the index of the matching `1`:

```
cols = rows.map {|row| z[row].each_with_index.select { |item, index| item == 1}.map(&:last) }
# => [[1], [3]]

```

If we want, we can then combine them using `zip`:

```
rows.zip(cols)
# => [[1, [1]], [2, [3]]]

```

The above works even if a row of `z` contains multiple occurrences of `1`

For example if we had:

```
z = [
  [1,0,0,1],
  [0,1,0,0],
  [0,0,0,1],
  [1,1,1,0]
]

```

then

```
rows = z.each_with_index.select { |row, index| row.include? 1}.map(&:last)
# => [0, 1, 2, 3]
cols = rows.map {|row| z[row].each_with_index.select { |item, index| item == 1}.map(&:last) }
# => [[0, 3], [1], [3], [0, 1, 2]]
rows.zip(cols)
# => [[0, [0, 3]], [1, [1]], [2, [3]], [3, [0, 1, 2]]]

```