java - How to find the saddlePoint in IntMatrix -
this question has answer here:
this program in finding saddle point of matrix (intmatrix). please me make method intmatrix m, parameters inside 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 requirement need saddlepoint method because have other methods
intmatrix class: //represents 2-dimensional matrix of integers constructor signature: intmatrix(int rows, int cols, int ... elements) //elements provided in row major order intmatrixutilityclass static methods: intmatrix sum(intmatrix ... matrices) //returns sum of arguments intmatrix product(intmatrix m1, intmatrix m2, intmatrix ... others) //returns product of arguments boolean[] saddlepoints(intmatrix m, int[] rows, int[] cols) /*for each of row , column pairs, returns true if specified element of m saddle point matrix; returns false otherwise*/
this program, need 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; (int = 0; < matrix.length; i++) { (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]; (intmatrix matrix : matrices) { int l = 0; (int = 0; < matrix.rows; i++) { (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; (int = 0; < m1.rows; i++) { (int j = 0; j < m2.cols; j++) { (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); (intmatrix other : others) { int length = others.length; l = 0; int[] result2 = new int[(m3.rows * others[length - 1].cols)]; (int = 0; < m3.rows; i++) { (int j = 0; j < other.cols; j++) { (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
first want please research on own before asking. here how find saddle point of matrix using java? can find accepted ans here tell not according wikipedia definition
sadle point:
a saddle point element of matrix both largest element in column , smallest element in row.
or in simple words matrix said have saddle point if entry a[x][y] smallest value in x'th row , largest value in y'th column. matrix may have more 1 saddle point.
this code according 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 = 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 @ (row,0) 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]; // } 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 =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 =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+" @ ("+(rowindx+1)+","+(colindx+1)+")"); } else { system.out.println("there no saddle point"); } } public static void main(string[] args) { saddlepoint sp = new saddlepoint(3, 4); sp.run(); } }
you can modify run method according needs. run method calculating saddle point
Comments
Post a Comment