Getting reference of a sub-matrix in java

后端 未结 4 1512
悲&欢浪女
悲&欢浪女 2020-12-18 17:26

I need to do parallel processing of sub-matrices recursively (original matrix divided into 4 passed into a method). The matrix is stored as a 2D array. I can\'t copy the ele

相关标签:
4条回答
  • 2020-12-18 17:34

    I would write a wrapper around the int[][] data and call it a Matrix class. Then write a method getSubMatrix(x, y, rows, cols). This is a simple Matrix class:

    static class Matrix {
        int[][] data;
        int x, y, columns, rows;
    
        public Matrix(int[][] data) {
            this(data, 0, 0, data.length, data[0].length);
        }
    
        private Matrix(int[][] data, int x, int y, int columns, int rows) {
            this.data = data;
            this.x = x;
            this.y = y;
            this.columns = columns;
            this.rows = rows;
        }
    
        public Matrix getSubMatrix(int x, int y, int columns, int rows) {
            return new Matrix(data, this.x + x , this.y + y, columns, rows);
        }
    
        public String toString() {
    
            StringBuffer sb = new StringBuffer();
    
            for (int i = y; i < x + rows; i++) {
                for (int j = x; j < x + columns; j++)
                    sb.append(data[i][j]).append(" ");
    
                sb.append("\n");
            }
            sb.setLength(sb.length() - 1);
    
            return sb.toString();
        }
    }
    

    This test program...:

    public static void main(String[] args) throws IOException {
    
        int[][] testData = new int[10][10];
    
        for (int i = 0; i < testData.length; i++) 
            for (int j = 0; j < testData[i].length; j++)
                testData[i][j] = 100 + i + j;
    
        Matrix full = new Matrix(testData);
    
        System.out.println("Full test matrix:");
        System.out.println(full);
    
        System.out.println();
    
        System.out.println("Part of the matrix:");
        System.out.println(full.getSubMatrix(3, 3, 3, 3));
    
    }
    

    ...prints:

    Full test matrix:
    100 101 102 103 104 105 106 107 108 109 
    101 102 103 104 105 106 107 108 109 110 
    102 103 104 105 106 107 108 109 110 111 
    103 104 105 106 107 108 109 110 111 112 
    104 105 106 107 108 109 110 111 112 113 
    105 106 107 108 109 110 111 112 113 114 
    106 107 108 109 110 111 112 113 114 115 
    107 108 109 110 111 112 113 114 115 116 
    108 109 110 111 112 113 114 115 116 117 
    109 110 111 112 113 114 115 116 117 118 
    
    Part of the matrix:
    106 107 108 
    107 108 109 
    108 109 110 
    
    0 讨论(0)
  • 2020-12-18 17:35

    I think you need some new class that will store List<List<Integer>>. In this case you will have probability make some sub lists and pass it other process.

    0 讨论(0)
  • 2020-12-18 17:41

    How about defining a template class as follows:

    template <class V, class I = int, class S = FullMatrix<V> >
    class Matrix{
    private:
        S m_structure; //The matrix structure
        I m_rowstart;//Row start index
        I m_columnstart;//Column start index
    }
    

    The main constructors would be

    Matrix();
    Matrix(size_t r, size_t c);//r rows and c columns
    Matrix(size_t r, size_t c, I rowStart, I columnStart);//rowstart and columnstart are given start indices
    Matrix(const Matrix<V, I, S>& source);
    

    You would then have functions to return the minimum/max row/column indices of form:

    I MinRowIndex() const;
    

    Next, you have functions to tell the number of rows/columns in the matrix.

    size_t Rows() const;
    

    Then, a function to allow replacement of elements in a row/column by another array of elements

    void Row(I row, const Array<V, I>& val);//Replace row
    

    Then, overload () to allow access to element in a given row and column

    const V& operator ()(I row, I column) const;//Get Element
    V& operator() (I row, I column);
    

    Although computational tests may need to be done to check the benefit of this method as compared to maintaining a big matrix and individual start/stop indices of various submatrices (as suggested by Lagerbaer in the other thread) one advantage here is that each submatrix is independent. They can be transposed, moved around, replaced. You probably may need to maintain a higher-level matrix structure in which this Matrix is a sub-structure.

    But it does seem to satisfy your question of being able to reference the submatrices independently.

    0 讨论(0)
  • 2020-12-18 17:59

    You already got an answer from Lagerbaer in the question that you reference. You just pass the array (which is a reference to the array in fact), and 4 extra parameters, which are the minimum and maximum x and y coordinates in the original array.

    0 讨论(0)
提交回复
热议问题