Multiply a matrix with another matrix that only contains elements on its diagonal. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Multiply a matrix with another matrix that only contains elements on its diagonal.

Demo Code


//package com.java2s;

public class Main {
    /**//  w  ww  .j a va  2  s.co  m
     * Multiply a matrix with another matrix that only contains elements on its
     * diagonal (0 elswhere).
     * @param mat The matrix (m x n).
     * @param diag The matrix that only contains elements on its diagonal, as a one-dimensional
     * vector of size n x 1 that represents the matrix of size n x n.
     * @return The resulting matrix of size m x n.
     */
    public static double[][] matrix_x_diagonalMatrix(double[][] mat,
            double[] diag) {
        final int m = mat.length;
        final int n = mat[0].length;

        double[][] res = new double[m][n];

        for (int row = 0; row < m; row++) {
            for (int col = 0; col < n; col++) {
                res[row][col] = mat[row][col] * diag[col];
            }
        }

        return res;
    }
}

Related Tutorials