Reputation: 1
This is my program in finding the Saddle Point of a Matrix (IntMatrix). Please help me make another method for IntMatrix m, for the Parameters inside the saddlePoints method?
public class SaddlePoint{
public void saddlePoints(IntMatrix m, int[] rows, int[] cols) {
int rows = m.length;
int cols = m[0].length;
boolean[][] flagArr = new boolean[rows][cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(m[i][j]==0){
flagArr[i][j]=true;
}
}
}
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(flagArr[i][j]==true){
/*for rows*/
for(int k=0; k<rows; k++){
m[k][j]=0;
}
/*for cols*/
for(int z=0; z<cols; z++){
m[i][z]=0;
}
}
}
}
}
}
this is the requirement but i need the saddlePoint method only because I already have the other methods
IntMatrix Class:
//represents a 2-dimensional matrix of integers
Constructor Signature:
IntMatrix(int rows, int cols, int ... elements)
//elements are provided in row major order
IntMatrixUtilityClass
Static Methods:
IntMatrix sum(IntMatrix ... matrices)
//returns the sum of its arguments
IntMatrix product(IntMatrix m1, IntMatrix m2, IntMatrix ... others)
//returns the product of its arguments
boolean[] saddlePoints(IntMatrix m, int[] rows, int[] cols)
/*for each of the row and column pairs, returns true if the specified element of m is a saddle
point for the matrix; returns false otherwise*/
this is my program, I only need the saddlePoints
public class IntMatrix {
private int[][] matrix;
private int rows;
private int cols;
private int[] elements;
public IntMatrix(int r, int c, int... e) {
this.rows = r;
this.cols = c;
this.elements = e;
matrix = new int[rows][cols];
int l = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
this.matrix[i][j] = elements[l];
l++;
}
}
}
public static IntMatrix sum(IntMatrix... matrices) {
int[] result = new int[matrices[0].rows * matrices[0].cols];
for (IntMatrix matrix : matrices) {
int l = 0;
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++) {
result[l] += matrix.matrix[i][j];
l++;
}
}
}
IntMatrix m3 = new IntMatrix(matrices[0].rows, matrices[0].cols, result);
return m3;
}
public static IntMatrix product(IntMatrix m1, IntMatrix m2,
IntMatrix... others) {
int[] result = new int[m1.rows * m2.cols];
int l = 0;
for (int i = 0; i < m1.rows; i++) {
for (int j = 0; j < m2.cols; j++) {
for (int k = 0; k < m1.cols; k++) {
result[l] += (m1.matrix[i][k] * m2.matrix[k][j]);
}
l++;
}
}
IntMatrix m3 = new IntMatrix(m1.rows, m2.cols, result);
for (IntMatrix other : others) {
int length = others.length;
l = 0;
int[] result2 = new int[(m3.rows * others[length - 1].cols)];
for (int i = 0; i < m3.rows; i++) {
for (int j = 0; j < other.cols; j++) {
for (int k = 0; k < m3.cols; k++) {
result2[l] += (m3.matrix[i][k] * other.matrix[k][j]);
}
l++;
}
}
m3 = new IntMatrix(m3.rows, others[length - 1].cols, result2);
}
return m3;
}
public String toString() {
return String.valueOf(rows) + " " + " " + String.valueOf(cols)
+ Arrays.toString(elements);
}
}// end of Matrix Class
Upvotes: -2
Views: 1080
Reputation: 950
First I want to say please do some research on your own before asking. From Here How to find a saddle point of a matrix using Java? you can find accepted ans but here I tell you that is not according to Wikipedia definition
A saddle point is an element of the matrix which is both the largest element in its
column and the smallest element in its row.
OR in simple words A matrix is said to have a saddle point if some entry a[x][y] is the smallest value in the x'th row and the largest value in the y'th column. A matrix may have more than one saddle point.
This Code is according to wikipedia defination
package com.mubasher.main;
import java.util.Random;
public class SaddlePoint {
private int[][] intMatrix;
private int[] colMaxima;
private int[] rowMinima;
public SaddlePoint(int col, int row){
intMatrix = new int[row][col];
colMaxima = new int[col];
rowMinima = new int[row];
fillMatrix();
}
private void fillMatrix() {
Random random = new Random();
for(int row = 0; row<intMatrix.length;row++){
for(int col = 0;col<intMatrix[0].length;col++){
intMatrix[row][col] = random.nextInt(21) - 10;
}
}
printMatrix();
}
private void printMatrix(int[][] intMatrix) {
for(int row = 0;row<intMatrix.length;row++){
for(int col = 0; col<intMatrix[0].length;col++){
System.out.print(intMatrix[row][col]+" ");
}
System.out.println("");
}
for(int i=0;i<intMatrix[0].length;i++)
System.out.print("----");
System.out.println("");
}
public void printMatrix() {
printMatrix(intMatrix);
}
public void printArray(int[] array,boolean isHorizontaly) {
for(int i = 0;i<array.length;i++){
if(isHorizontaly){
System.out.print(array[i]+" ");
} else {
System.out.println(array[i]);
}
}
if(isHorizontaly){System.out.println("");
for(int i=0;i<array.length;i++)
System.out.print("----");
} else {
System.out.println("----");
}
System.out.println("");
}
public void run(){
int maxVal = 0,minVal=0;
//minimum in each row
for(int row = 0; row<intMatrix.length;row++){
for(int col = 0;col<intMatrix[0].length;col++){
if(col == 0 ) {
rowMinima[row]=intMatrix[row][col]; // assume first val at (row,0) is minimum
} else {
if(intMatrix[row][col]<rowMinima[row]){
rowMinima[row]=intMatrix[row][col]; // assign new minimum val
}
}
}
}
//maximum in each column
for(int col = 0; col<intMatrix[0].length;col++){
for(int row = 0;row<intMatrix.length;row++){
if(row == 0 ) {
colMaxima[col]=intMatrix[row][col]; // for
} else {
if(intMatrix[row][col]>colMaxima[col]){
colMaxima[col]=intMatrix[row][col]; // assign new max val
}
}
}
}
printArray(colMaxima,true);
printArray(rowMinima,false);
int colIndx=0,rowIndx=0;
for(int i =0;i<colMaxima.length;i++){
if(i == 0 ) {
minVal= colMaxima[i];
colIndx=i;
} else {
if(colMaxima[i]<minVal){
minVal= colMaxima[i];
colIndx=i;
}
}
}
for(int i =0;i<rowMinima.length;i++){
if(i == 0 ) {
maxVal= rowMinima[i];
rowIndx = i;
} else {
if(rowMinima[i]>maxVal){
maxVal= rowMinima[i];
rowIndx = i;
}
}
}
if(minVal == maxVal){
System.out.println("We Have Saddle Point "+maxVal+" at ("+(rowIndx+1)+","+(colIndx+1)+")");
} else {
System.out.println("There is no saddle point");
}
}
public static void main(String[] args) {
SaddlePoint sp = new SaddlePoint(3, 4);
sp.run();
}
}
you can modify run method according to your needs. run method is calculating saddle point
Upvotes: 0