multiply a 3x1 matrix with a 1x3 matrix in csharp

To multiply a 3x1 matrix with a 1x3 matrix in C#, you can use the dot product formula, which multiplies corresponding elements and sums the results.

Here's an example code snippet:

main.cs
int[,] matrix1 = new int[3, 1] { { 2 }, { 3 }, { 4 } };
int[,] matrix2 = new int[1, 3] { { 5, 6, 7 } };

int[,] result = new int[3, 3];

// Multiply matrices
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        for (int k = 0; k < 1; k++)
        {
            result[i, j] += matrix1[i, k] * matrix2[k, j];
        }
    }
}

// Print result
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        Console.Write(result[i, j] + " ");
    }
    Console.WriteLine();
}
510 chars
27 lines

In the code above, we first define the two matrices (using 2D arrays), and then create a third array to hold the result of the multiplication.

We then use three nested loops to compute the value of each element of the result matrix, using the dot product formula. Finally, we print the result matrix to the console.

Note that this implementation assumes that the input matrices have compatible dimensions (i.e., the number of columns of the first matrix matches the number of rows of the second matrix). It also assumes that the matrices are composed of integers.

gistlibby LogSnag