First transposes matrix matA, then multiplies it with matrix matB. - Java java.lang

Java examples for java.lang:Math Matrix

Description

First transposes matrix matA, then multiplies it with matrix matB.

Demo Code


//package com.java2s;

public class Main {
    /**//  w w w  .ja  va2  s.c  o m
     * First transposes matrix matA, then multiplies it with matrix matB.
     * @param matA The matrix (m x n).
     * @param matB The matrix (m x r).
     * @return The resulting matrix of size n x r.
     */
    public static double[][] matrixATrans_x_matrixB(double[][] matA,
            double[][] matB) {
        final int m = matA[0].length;
        final int n = matB[0].length;

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

        for (int r = 0; r < m; r++) {
            for (int c = 0; c < n; c++) {
                for (int i = 0; i < matA.length; i++) {
                    res[r][c] += matA[i][r] * matB[i][c];
                }
            }
        }
        return res;
    }
}

Related Tutorials