Depending on the name of your question, here is a solution that looks for positions of zero values ( fixate ), then actually nulls the corresponding row and column ( clear , matches the contents of your question more)
def fixate matrix, search=0, replace=0 rcs = [] matrix.each_with_index do |row,r| row.each_with_index do |col,c| rcs << [ r, c ] if col == search end end rcs.each do |(row, col)| clear matrix, row, col, replace end matrix end def clear matrix, row, col, val=0 matrix[row].map! { |_| val }
Quick test:
fixate [ # [ [ 1, 2, 3, 4 ], # [ 1, 2, 0, 4 ], [ 5, 6, 7, 8 ], # [ 5, 6, 0, 8 ], [ 9, 10, 0, 11 ], # [ 0, 0, 0, 0 ], [ 12, 13, 14, 15 ] # [ 12, 13, 0, 15 ] ] # ]