-2

这是我寻找矩阵鞍点的程序(IntMatrix)。请帮我为 IntMatrix m 制作另一种方法,用于 saddlePoints 方法中的参数?

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;
                    }
                }
            }
        }
    }
}

这是要求,但我只需要 saddlePoint 方法,因为我已经有了其他方法

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*/

这是我的程序,我只需要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 
4

1 回答 1

0

首先我想说请在提问之前自己做一些研究。从这里如何使用Java找到矩阵的鞍点?你可以找到接受的 ans 但在这里我告诉你这不符合维基百科的定义

鞍点:

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 简单来说,如果某个条目 a[x][y] 是第 x 行中的最小值和第 y 列中的最大值,则称矩阵具有鞍点。一个矩阵可能有多个鞍点。

此代码根据维基百科定义

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();
}

}

您可以根据需要修改运行方法。run 方法正在计算鞍点

于 2014-09-24T08:02:21.483 回答