Matlab高級教程_第二篇:MATLAB和C#對應數據類型的講解(多講一點兒C#的矩陣運算)


1. MATLAB對應C#的數據類型主要在引入的父類庫MWArray當中。有如下對應規則

.NET TYPE      MWArrayTYPE    MATLAB Type

System.Double     MWNumericArray    double

System.Number    MWNumericArray    double

System.Float     MWNumericArray     single

System.Byte      MwNumbericArray    int8

System.Short     MWNumbericArray     int16

System.int32      MWNumbericArray     int32

system.int64      MWNumbericArray     int64

System.Char     MWCharArray          char

System.String       MWCharArray         char

System.Boolean    MWLogicArray       logical

N/A           MWStructArray      structure

N/A          MWCellArray       cell

 

2. MWArray是MathWorks公司專門為.Net開發的一個.Net組件,用於條用MCR進行m函數進行計算。是.Net和matlab混編的橋梁之一。

3. C#怎么向MATLAB傳遞參數?怎么把MATLAB計算好的結果,轉換到C#中?

           //混合編程傳遞參數測試
            //1. 假設M函數已經寫好,並且編譯完成
            //傳遞參數之前要搞清楚什么問題?
            //要傳遞的參數實際類型:N=5,double[] 
            //MWArray,怎么把.net中的數據傳遞給MWArray?

            //2.要傳遞數值類型:double,int,byte...
            //單個值可以隱式轉換
            MWArray m1 = 5;
            int N = 5;
            MWArray m2 = N;
            MWArray str = "my name is matlab.net";


            //3.如果M函數需要一個數組(一維數組)
            //多個值不能通過隱式轉換,因此這個是錯誤的,也不能強制轉換!!
            //MWArray m3 = new int[] {1,2,3,4 };
            //因此!要通過子類的MWNumericArray的數組類型先要進行轉換。
            //因此!.net數值類型的數組可以隱式轉換為MWNumericArray。
            //MWNumericArray他是MWArray的子類
            MWNumericArray array1 = new int[] { 112, 22, 44, 88 };
            //通過做子類型再轉換成父類型。
            MWArray array2 = array1;
            //因此!可以通過用子類型的數組進行強制轉換
            MWArray array3 = (MWNumericArray)new int[] { 112, 22, 44, 88 };


            //4.如果M函數需要一個多維數組
            MWNumericArray array4 = new int[,] { {112, 22}, {44, 88} };
            MWArray array5 = array4;
            //或者
            MWArray array6 = (MWNumericArray)new int[,] { { 112, 22 }, { 44, 88 } };
            //Console.WriteLine(array5);

            //5. MWNumericArray的構造函數
            MWNumericArray array7 = new MWNumericArray(new int[] { 2, 35, 6 }); //全0
            MWNumericArray array8 = new MWNumericArray(2,3,new double[] { 1, 3, 5, 4, 5, 6 }); //兩行2列的double數組
            //Console.WriteLine(array8);
            //1 3 5
            //4 5 6

4. 矩陣運算導入類Matrix,這個有現成的寫好的類,可以聯系作者要(不過寫的有問題,后面生成MATLAB的矩陣運算方式)

            //6. 矩陣運算
            //6.1 實例化矩陣,並規定是幾行幾列的矩陣
            //注意:矩陣維度是從0開始的
            Console.WriteLine("這是第一個矩陣");
            Matrix matrix = new Matrix(2,2);  //2行 2列
            //賦值方式
            matrix[0, 0] = 3;
            matrix[0, 1] = 4;
            matrix[1, 0] = 8;
            matrix[1, 1] = 6;
            //Console.WriteLine(matrix[0, 0]);
            //Console.WriteLine(matrix[0, 1]);
            //Console.WriteLine(matrix[1, 0]);
            //Console.WriteLine(matrix[1, 1]);
            //6.2打印矩陣
            matrix.Print();
            Console.WriteLine("\n");
            //6.3 矩陣的四則運算
            Console.WriteLine("這是第二個矩陣");
            Matrix matrix1 = new Matrix(2, 2);  //2行 2列
            //賦值方式
            matrix1[0, 0] = 4;
            matrix1[0, 1] = 40;
            matrix1[1, 0] = 80;
            matrix1[1, 1] = 60;
            matrix1.Print();
            Console.WriteLine("\n");

            Console.WriteLine("這是第三個矩陣");
            Matrix matrix2 = new Matrix(3, 3);  //3行 3列
            //賦值方式
            matrix2[0, 0] = 4;
            matrix2[0, 1] = 40;
            matrix2[0, 2] = 40;
            matrix2[1, 0] = 30;
            matrix2[1, 1] = 10;
            matrix2[1, 2] = 60;
            matrix2[2, 0] = 80;
            matrix2[2, 1] = 50;
            matrix2[2, 2] = 60;
            matrix2.Print();
            Console.WriteLine("\n");

            //加法
            Console.WriteLine("矩陣的加法的結果");
            var result = matrix + matrix1;
            result.Print();
            Console.WriteLine("\n");

            //減法
            Console.WriteLine("矩陣的減法的結果");
            var result2 = matrix - matrix1;
            result2.Print();
            Console.WriteLine("\n");

            //乘法
            Console.WriteLine("矩陣的乘法的結果");
            var result3 = matrix * matrix2;
            result3.Print();
            Console.WriteLine("\n");

            //除法沒有

  

5.繼續進行講解

            //6. 定義字符
            MWCharArray ch = "Hello Matlab";
            MWCharArray ch1 = new MWCharArray(new string[] { "Hellow","World!"}); //傳遞一個字符串數組

            //7. 定義struct結構體
            MWStructArray stru = new MWStructArray(new int[] { 2, 2 }, new string[] { "Name", "Age" });
            stru["Name", new int[] { 1, 1 }] = "Tom";
            stru["Age", new int[] { 1, 1 }] = 18;
            Console.WriteLine(string.Format("Name:{0},Age:{1}",stru["Name",new int[] { 1,1}].ToString(),stru["Age",new int[] {1,1 }]));
            //MWArray下標訪問方式跟MATLAB一樣。

            //8.元組的自己看吧

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM