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]]]
arr =[[0,0....]]none of that would be needed--answers would have just referencedarr. I realize you're new to SO. This is just a tip, not a criticism.