Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

.net - How to multiply a matrix in C#?

I cannot get this method to work. It intends to multiply a matrix by a given one. Could someone help me to correct it please?

    class Matriz
    {
        public double[,] structure;

        //Other class methods

        public void multiplyBy(Matrix m)
        {
            if (this.structure.GetLength(1) == m.structure.GetLength(0))
            {
                Matriz resultant = new Matriz(this.structure.GetLength(0), m.structure.GetLength(1));
                for (int i = 0; i < this.structure.GetLength(0) - 1; i++)
                {
                    for (int j = 0; j < m.structure.GetLength(1) - 1; j++)
                    {
                        resultant.structure[i, j] = 0;
                        for (int z = 0; z < this.structure.GetLength(1) - 1; z++)
                        {
                            resultant.structure[i, j] += this.structure[i, z] * m.structure[z, j];
                        }
                    }
                }
                this.structure= resultant.structure;
            }
            else
            {
                Console.WriteLine("Selected matrixs cannot be multiply");
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read this MSDN Magazine article by James McCaffrey and use the code below as an extension method. They use a jagged array which is more convenient and sometimes faster than a 2D array. Change any data[i][j] to data[i,j] to make the code work with a 2D array.

public static double[] MatrixProduct(this double[][] matrixA,
    double[] vectorB)
{
    int aRows=matrixA.Length; int aCols=matrixA[0].Length;
    int bRows=vectorB.Length;
    if (aCols!=bRows)
        throw new Exception("Non-conformable matrices in MatrixProduct");
    double[] result=new double[aRows];
    for (int i=0; i<aRows; ++i) // each row of A
        for (int k=0; k<aCols; ++k)
            result[i]+=matrixA[i][k]*vectorB[k];
    return result;
}
public static double[][] MatrixProduct(this double[][] matrixA,
    double[][] matrixB)
{
    int aRows=matrixA.Length; int aCols=matrixA[0].Length;
    int bRows=matrixB.Length; int bCols=matrixB[0].Length;
    if (aCols!=bRows)
        throw new Exception("Non-conformable matrices in MatrixProduct");
    double[][] result=MatrixCreate(aRows, bCols);
    for (int i=0; i<aRows; ++i) // each row of A
        for (int j=0; j<bCols; ++j) // each col of B
            for (int k=0; k<aCols; ++k)
                result[i][j]+=matrixA[i][k]*matrixB[k][j];
    return result;
}
public static double[][] MatrixCreate(int rows, int cols)
{
    // creates a matrix initialized to all 0.0s  
    // do error checking here?  
    double[][] result=new double[rows][];
    for (int i=0; i<rows; ++i)
        result[i]=new double[cols];
    // auto init to 0.0  
    return result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...