Doubling a Matrix

前端 未结 3 750
后悔当初
后悔当初 2021-01-26 08:21

I\'ve created a two dimensional matrix and populated it with random numbers. I\'ve then printed it out. I need help creating a second matrix that is twice the size of the first,

相关标签:
3条回答
  • 2021-01-26 08:33

    I'm no sure if that is what you are looking for but try this:

    for (int i = 0; i < newMatrix.length; i++) {
        for (int j = 0; j < newMatrix.length; j++) {
            newMatrix[i][j] = matrix[i/size][j/size];
        }
    }
    

    Note: This code is surely not the best solution but a fast an easy one. It only works if both dimensions are the same size and it won't work if newMatrix is not exactly two times matrix. If it's always just to "double" a matrix it should work fine.


    Output:
    If you choose size 2 than it will output:

    Enter the size of the matrix
    2
    The Matrix is
    3 5 
    5 2 
    The newMatrix is
    3 3 5 5 
    3 3 5 5 
    5 5 2 2 
    5 5 2 2 
    

    and for size 3 it would be for example

    Enter the size of the matrix
    3
    The Matrix is
    4 4 3
    5 9 4
    7 4 1
    The newMatrix is
    4 4 4 4 3 3
    4 4 4 4 3 3
    5 5 9 9 4 4
    5 5 9 9 4 4
    7 7 4 4 1 1
    7 7 4 4 1 1
    

    It's not clear what you are asking but I hope this helps (:

    0 讨论(0)
  • 2021-01-26 08:36

    In Java 8 you can handle this pretty easily using maps and collectors. Here is a full example:

    public class DoubleMatrix {
      public static void main(String[] args) {
        List<List<Integer>> startingMatrix = Arrays.asList(
            Arrays.asList(3, 4),
            Arrays.asList(2, 1)
        );
    
        List<List<Integer>> doubleMatrix
            = startingMatrix.stream()
                            .map(innerList -> { //For each list
                              List<Integer> doubled = innerList.stream()
                                                               .map(element -> Arrays.asList(element, element)) //Return a list doubling each element
                                                               .flatMap(l -> l.stream())  //Flatten out/join all doubled lists
                                                               .collect(Collectors.toList());
                              return Arrays.asList(doubled, doubled); //Double the list
                            })
                            .flatMap(l -> l.stream())
                            .collect(Collectors.toList()); //Collect into a final list
    
        System.out.println(doubleMatrix);
      }
    }
    

    This avoids needing to know the size of the list beforehand, and is also tolerant of there being a difference between the width and height of your matrix - simply doubling every element in both directions.

    0 讨论(0)
  • 2021-01-26 08:54
    import java.util.Scanner;
    import java.util.Random;
    
    
    public class MatrixDoubler {
        public static void main(String[] arg) {
            Scanner keyboard = new Scanner(System.in);
            Random rand = new Random();
    
            System.out.println("Enter the size of the matrix");
    
            int size = keyboard.nextInt();
            int A[][] = new int[size][size];
    
            for (int row = 0; row < size; ++row) {
                for (int col = 0; col < size; ++col) {
                    A[row][col] = rand.nextInt(10);
                }
            }
    
            System.out.println("Matrix A:");
            printMatrix(A);
    
            int[][] B = doubleMatrix(A);
    
            System.out.println("Matrix B:");
            printMatrix(B);
        }
    
        private static int[][] doubleMatrix(int[][] A) {
            int rows = A.length;
            assert(rows > 0);
            int cols = A[0].length;
            assert(cols > 0);
    
            int B[][] = new int[rows * 2][cols * 2];
    
            for (int row = 0; row < rows * 2; ++row) {
                for (int col = 0; col < cols * 2; ++col) {
                    B[row][col] = A[row / 2][col / 2];
                }
            }
    
            return B;
        }
    
        private static void printMatrix(int[][] M) {
            for(int i = 0; i < M.length; i++) {
                for(int j = 0; j < M.length; j++) {
                    System.out.print(M[i][j] + " ");
                }
    
                System.out.println();   
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题