使用标量乘法乘以2D矩阵

时间:2017-09-10 20:29:56

标签: java matrix multidimensional-array

我必须编写一个程序,提示用户输入2D矩阵的尺寸以及矩阵的值。

之后,我必须将矩阵乘以2并打印结果。

我已经完成了程序,但我无法弄清楚如何将矩阵相乘并将这些值存储到新矩阵中。到目前为止,这是我的代码:

import java.util.Scanner; 

public class MatrixMultiplication {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in); 

    System.out.println("Please enter the number of rows: ");
       int row = sc.nextInt();

    System.out.println("Please enter the number of columns: ");
       int col = sc.nextInt();

    int[][] matrix = new int[row][col];

    System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: ");
    for (row = 0; row < matrix.length; row++) {
        for (col = 0; col < matrix[row].length; col++) {
            matrix[row][col] = sc.nextInt(); 
        }
    }
    System.out.println();

    for (row = 0; row < matrix.length; row++) {
        for (col = 0; col < matrix[row].length; col++) {
            System.out.print(matrix[row][col] + " ");
        }
        System.out.println(); 
    } 
}
} 

我到处寻求帮助,并尝试了几种不同的陈述,但似乎没有一个是正确的。

我知道我必须使用for循环,但就像我说的那样,我不完全确定要使用多少,如何将新值存储在矩阵中并显示它等等。

任何方向都将不胜感激!

3 个答案:

答案 0 :(得分:0)

首先,您必须尝试自己解决问题,在考虑超过3小时后,您可以在互联网上搜索解决方案

您需要以这种方式更改最后一个循环:

for (row = 0; row < matrix.length; row++) {
        for (col = 0; col < matrix[row].length; col++) {
            System.out.print(matrix[row][col]*2 + " ");
        }
        System.out.println(); 
} 

我在这一行的循环中将它乘以2:

System.out.print(matrix[row][col]*2 + " ");

答案 1 :(得分:0)

在第一个double-for-loop之后,您需要声明second matrix哪个元素将是*2的第一个元素,所以:

int[][] matrixDouble = new int[row][col];
for (row = 0; row < matrix.length; row++) {
    for (col = 0; col < matrix[row].length; col++) {
        matrixDouble[row][col] = matrix[row][col]*2; //element -> *2 -> store in new matrix
        System.out.print(matrixDouble[row][col] + " ");
    }
    System.out.println(); 
} 

在新matrix的每个框中,它会将数字存储在第一个矩阵中的相同位置,但2因子

您可以同时打印

取决于您需要如何解决它,但您可以在第一个for-loop中移动操作:

for (row = 0; row < matrix.length; row++) {
    for (col = 0; col < matrix[row].length; col++) {
        matrix[row][col] = sc.nextInt(); 
        matrixDouble[row][col] = matrix[row][col]*2;
    }
}

答案 2 :(得分:0)

为什么不定义Matrix类并使用它?

这是一个Matrix.java:

import java.io.*;

public class Matrix {
        private double[][] matrix;
        private int rows;
        private int cols;

        public Matrix ( double [][] m ) {
                rows   = m.length;
                cols   = m[0].length;
                matrix = m;
        }

        public Matrix ( Matrix m  ) {
                rows   = m.rows;
                cols   = m.cols;
                matrix = new double[rows][cols];
                for ( int i=0; i<rows; i++ ) {
                        for ( int j=0; j<cols; j++ ) {
                                matrix[i][j] = m.matrix[i][j];
                        }
                }    
        }

        public void scale( double M ) {
                for ( int i=0; i<rows; i++ ) {
                        for ( int j=0; j<cols; j++ ) {
                                matrix[i][j] = matrix[i][j] * M;
                        }
                }
        }

        public double[][] getMatrix() {
                return matrix;
        }

        public void print () {
                for ( int i=0; i<rows; i++ ) {
                        for ( int j=0; j<cols; j++ ) {
                                System.out.print( matrix[i][j] + ", " );
                        }
                        System.out.println();
                }
        }
}

这是一个演示Matrix的测试程序:

import java.io.*;
public class RunMatrix {
        public static void main(String args[]) {
                System.out.println( "Starting RunMatrix." );
                double [][] a = new double[][]{
                        { 1.1, 1.2, 1.3 },
                        { 2.1, 2.2, 2.3 },
                        { 3.1, 3.2, 3.3 },
                };
                Matrix m = new Matrix(a);
                m.print();

                System.out.println( "Scale by 2." );
                m.scale(2.0);
                m.print();

                Matrix n = new Matrix(m);
                System.out.println( "here is n." );
                n.print();

                System.out.println( "Scale n by 2." );
                n.scale(2.0);
                n.print();

                System.out.println( "m is still." );
                m.print();
        }
}

以上是上述输出:

Starting RunMatrix.
1.1, 1.2, 1.3,
2.1, 2.2, 2.3,
3.1, 3.2, 3.3,
Scale by 2.
2.2, 2.4, 2.6,
4.2, 4.4, 4.6,
6.2, 6.4, 6.6,
here is n.
2.2, 2.4, 2.6,
4.2, 4.4, 4.6,
6.2, 6.4, 6.6,
Scale n by 2.
4.4, 4.8, 5.2,
8.4, 8.8, 9.2,
12.4, 12.8, 13.2,
m is still.
2.2, 2.4, 2.6,
4.2, 4.4, 4.6,
6.2, 6.4, 6.6,
相关问题