Created
July 2, 2015 04:09
-
-
Save tancnle/74e5d9911fb70c8490b8 to your computer and use it in GitHub Desktop.
Find saddle points from given matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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