【问题标题】:Multiply a 2D matrix using scalar multiplication使用标量乘法将二维矩阵相乘
【发布时间】:2017-09-10 20:29:56
【问题描述】:

我必须编写一个程序,提示用户输入二维矩阵的维度以及矩阵的值。

之后,我必须将矩阵乘以 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 循环,但就像我说的我不完全确定要使用多少,如何将新值存储在矩阵中并显示它等等。

任何方向将不胜感激!

【问题讨论】:

标签: java matrix multidimensional-array


【解决方案1】:

在第一个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】:

首先你要自己尝试解决问题,思考超过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 + " ");

【讨论】:

  • 3 hours...只是好奇这是从哪里来的?
  • @tima 当我开始解决此类问题时,我尝试编写一个显示数字是否为素数的程序,我尝试在不搜索互联网的情况下自己编写它,我花了3 小时:D
  • @tima 在那种情况下,我学会了如何使用flagbreak 是如何工作的,return 在方法中的真正含义
  • 我确实盯着屏幕看了一会儿,试图弄清楚这一点,哈哈。感谢您的帮助!
【解决方案3】:

为什么不定义一个 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,

【讨论】:

    猜你喜欢
    • 2012-12-27
    • 2014-02-28
    • 1970-01-01
    • 2021-08-14
    • 2021-12-26
    • 1970-01-01
    • 2020-07-18
    • 2020-03-16
    • 1970-01-01
    相关资源
    最近更新 更多