How to multiply matrix by vector in C#

1 Answer

0 votes
using System;
 
class Program
{
    static void matrix_x_vector(int[,] matrix, int[] vector, int[] multiplied_array) {
        int rows = matrix.GetLength(0);  
        int cols = matrix.GetLength(1);  
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                multiplied_array[i] += matrix[i, j] * vector[j];
            }
        }
    }

    static void Main() {
        int[,] matrix = { {0, 3, 5},
                          {5, 7, 2} };
        int[] vector = {2, 4, 3};                      

        int[] multiplied_array = {0, 0};   
  
        matrix_x_vector(matrix, vector, multiplied_array);
  
        foreach (int n in multiplied_array) {
            Console.Write(n + " ");
        }
    }
}
 
 
 
 
 
/*
run:
   
27 44 
   
*/

 



answered Mar 27, 2022 by avibootz
edited Mar 27, 2022 by avibootz

Related questions

1 answer 120 views
120 views asked Mar 28, 2022 by avibootz
1 answer 85 views
85 views asked Mar 24, 2022 by avibootz
2 answers 136 views
136 views asked Mar 23, 2022 by avibootz
1 answer 146 views
146 views asked Mar 23, 2022 by avibootz
1 answer 152 views
2 answers 246 views
246 views asked Jun 1, 2014 by avibootz
1 answer 139 views
...