Skip to content

Instantly share code, notes, and snippets.

@tancnle
Created July 2, 2015 04:09
Show Gist options
  • Save tancnle/74e5d9911fb70c8490b8 to your computer and use it in GitHub Desktop.
Save tancnle/74e5d9911fb70c8490b8 to your computer and use it in GitHub Desktop.
Find saddle points from given matrix
require 'matrix'
matrix = Matrix[
[9, 2, 8, 6],
[5, 3, 1, 5],
[6, 8, 7, 9]
]
cols = matrix.column_vectors
rows = matrix.row_vectors
saddle_points = []
matrix.each_with_index do |point, row, col|
if (point == rows[row].max) &&
(point == cols[col].min)
saddle_points << [row, col]
end
end
saddle_points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment