⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csharpmatlabcompilerver4.cs

📁 MATLAB C# Book. This book is a great tutorial for C# programmers who use MATLAB to develop applicati
💻 CS
📖 第 1 页 / 共 2 页
字号:
			}
			catch 
			{
				System.Console.Write("We cannot create a pointer mxArray. Please check again." ) ;
				return (IntPtr)0;
			}


			mxPointerTempReal = Marshal.AllocHGlobal(dbVectorReal.Length * Marshal.SizeOf(dbVectorReal[0]));
			mxPointerTempImag = Marshal.AllocHGlobal(dbVectorImag.Length * Marshal.SizeOf(dbVectorImag[0]));

			Marshal.Copy(dbVectorReal, 0, mxPointerTempReal, dbVectorReal.Length);
			Marshal.Copy(dbVectorImag, 0, mxPointerTempImag, dbVectorImag.Length);

			mxSetPr(mxPointerBuffer, mxPointerTempReal);
			mxSetPi(mxPointerBuffer, mxPointerTempImag);

			mxPointer = mxDuplicateArray(mxPointerBuffer);

			Marshal.FreeHGlobal(mxPointerTempReal);
			Marshal.FreeHGlobal(mxPointerTempImag);

			return mxPointer;	
	
		}


		public static double[] mxArray2double_vectorReal(IntPtr mxPointer)
		{
			// convert a real term of an mxArray mxPointer to a double vector
			int vectorSize ;

			int row = mxGetM(mxPointer) ;
			int col = mxGetN(mxPointer) ;
			if (row >col)	{ vectorSize = row ;	}	
			else			{ vectorSize = col ;	}


			IntPtr PointerVector;

			double [] dbVector ;
			dbVector = new double[vectorSize] ;

			PointerVector = mxGetPr(mxPointer);
			Marshal.Copy(PointerVector, dbVector, 0, vectorSize);

			return dbVector;

		}

		public static void mxArray2double_vectorComplex(IntPtr mxPointer, ref double[] dbVectorReal, ref double[] dbVectorImag)
		{
			// convert a complex mxArray mxPointer to double vectors
			int vectorSize ;

			int row = mxGetM(mxPointer) ;
			int col = mxGetN(mxPointer) ;
			if (row >col)	{ vectorSize = row ;	}	
			else			{ vectorSize = col ;	}


			// for real term
			IntPtr PointerVectorReal;

			PointerVectorReal = mxGetPr(mxPointer);
			Marshal.Copy(PointerVectorReal, dbVectorReal, 0, vectorSize);


			// for imaginary term
			IntPtr PointerVectorImag;
			int i ;

			try 
			{
				PointerVectorImag = mxGetPi(mxPointer);
				Marshal.Copy(PointerVectorImag, dbVectorImag, 0, vectorSize); 
			}
			catch
			{
				for ( i=0; i<vectorSize; i++) 
				{
					dbVectorImag[i] = 0.0 ;
				}
			}


		}


	// MATRIX
		public static IntPtr double2mxArray_matrixReal(double[,] dbMatrix)
		{				
			// convert a double matrix to an mxArray mxPointer 

			int i, j, row, col, index ;
			row = dbMatrix.GetUpperBound(0) - dbMatrix.GetLowerBound(0) + 1;
			col = dbMatrix.GetUpperBound(1) - dbMatrix.GetLowerBound(1) + 1;

			IntPtr mxPointer		= (IntPtr)0 ;
			IntPtr mxPointerBuffer	= (IntPtr)0 ;
			IntPtr mxPointerTemp ;

			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(row, col, MatlabCSharp.mxComplexity.mxREAL);
			}
			catch 
			{
				System.Console.Write("We cannot create a pointer mxArray. Please check again." ) ;
				return (IntPtr)0;
			}

			double[] dbVectorBuffer;
			dbVectorBuffer = new double[ row*col ];


			//transfer double matrix to double vector
			for( i=0; i<row; i++)  
			{
				for( j=0; j<col; j++)  
				{
					index  = j*row + i ;
					dbVectorBuffer[index] = dbMatrix[i, j];
				}
			}

			mxPointerTemp = Marshal.AllocHGlobal(dbVectorBuffer.Length * Marshal.SizeOf(dbVectorBuffer[0]));
			Marshal.Copy(dbVectorBuffer, 0, mxPointerTemp, dbVectorBuffer.Length);

			mxSetPr(mxPointerBuffer, mxPointerTemp);

			mxPointer = mxDuplicateArray(mxPointerBuffer);
			Marshal.FreeHGlobal(mxPointerTemp);

			return mxPointer;
	
		}


		public static IntPtr double2mxArray_matrixComplex(double[,] dbMatrixReal, double[,] dbMatrixImag)
		{				
			// convert a double matrixes to a complex mxArray mxPointer 

			int i, j, row, col, index ;
			row = dbMatrixReal.GetUpperBound(0) - dbMatrixReal.GetLowerBound(0) + 1;
			col = dbMatrixReal.GetUpperBound(1) - dbMatrixReal.GetLowerBound(1) + 1;

			IntPtr mxPointer		= (IntPtr)0 ;
			IntPtr mxPointerBuffer	= (IntPtr)0 ;

			IntPtr mxPointerTempReal ;
			IntPtr mxPointerTempImag ;

			try
			{
				mxPointerBuffer = mxCreateDoubleMatrix(row, col, MatlabCSharp.mxComplexity.mxCOMPLEX);
			}
			catch 
			{
				System.Console.Write("We cannot create a pointer mxArray. Please check again." ) ;
				return (IntPtr)0;
			}

			double[] dbVectorBufferReal;
			dbVectorBufferReal = new double[ row*col ];

			double[] dbVectorBufferImag;
			dbVectorBufferImag = new double[ row*col ];


			//transfer double matrix to double vector
			for( i=0; i<row; i++)  
			{
				for( j=0; j<col; j++)  
				{
					index  = j*row + i ;
					dbVectorBufferReal[index] = dbMatrixReal[i, j];
					dbVectorBufferImag[index] = dbMatrixImag[i, j];
				}
			}


			mxPointerTempReal = Marshal.AllocHGlobal(dbVectorBufferReal.Length * Marshal.SizeOf(dbVectorBufferReal[0]));
			mxPointerTempImag = Marshal.AllocHGlobal(dbVectorBufferImag.Length * Marshal.SizeOf(dbVectorBufferImag[0]));
			
			Marshal.Copy(dbVectorBufferReal, 0, mxPointerTempReal, dbVectorBufferReal.Length);
			Marshal.Copy(dbVectorBufferImag, 0, mxPointerTempImag, dbVectorBufferImag.Length);

			mxSetPr(mxPointerBuffer, mxPointerTempReal);
			mxSetPi(mxPointerBuffer, mxPointerTempImag);

			mxPointer = mxDuplicateArray(mxPointerBuffer);

			Marshal.FreeHGlobal(mxPointerTempReal);
			Marshal.FreeHGlobal(mxPointerTempImag);

			return mxPointer;
	
		}


		public static double[,] mxArray2double_matrixReal(IntPtr mxPointer)
		{
			// convert a real term of an mxArray mxPointer to a double matrix

			int i, j, index, row, col ;
			row = mxGetM(mxPointer);	
			col = mxGetN(mxPointer);

			IntPtr PointerDbArray;

			double [,] dbRealMatrix ;
			dbRealMatrix = new double[row, col] ;

			PointerDbArray = mxGetPr(mxPointer);

			double[] dbBufferVector ;
			dbBufferVector = new double[ row*col ];


			Marshal.Copy(PointerDbArray, dbBufferVector, 0, row*col);

			// convert vector to matrix
			for( i=0; i<row; i++)
			{
				for( j=0; j<col; j++) 
				{
					index  = j*row + i ;
					dbRealMatrix[i,j] = dbBufferVector[index];
				}
			}
			

			return dbRealMatrix;

		}
		

		public static void mxArray2double_matrixComplex(IntPtr mxPointer, ref double[,] dbMatrixReal, ref double[,] dbMatrixImag)
		{
			// convert a complex mxArray mxPointer to double matrixes

			int i, j, index, row, col ;
			row = mxGetM(mxPointer);	
			col = mxGetN(mxPointer);


			// for real term
			IntPtr PointerDbArrayReal;

			PointerDbArrayReal = mxGetPr(mxPointer);

			double[] dbBufferVectorReal ;
			dbBufferVectorReal = new double[ row*col ];


			Marshal.Copy(PointerDbArrayReal, dbBufferVectorReal, 0, row*col);

					// convert vector to matrix
			for( i=0; i<row; i++)
			{
				for( j=0; j<col; j++) 
				{
					index  = j*row + i ;
					dbMatrixReal[i,j] = dbBufferVectorReal[index];
				}
			}


			// for imaginary term
			IntPtr PointerDbArrayImag;

			try 
			{
				PointerDbArrayImag = mxGetPi(mxPointer);

				double[] dbBufferVectorImag ;
				dbBufferVectorImag = new double[ row*col ];


				Marshal.Copy(PointerDbArrayImag, dbBufferVectorImag, 0, row*col);

				// convert vector to matrix
				for( i=0; i<row; i++)
				{
					for( j=0; j<col; j++) 
					{
						index  = j*row + i ;
						dbMatrixImag[i,j] = dbBufferVectorImag[index];
					}
				}
			}

			catch 
			{
				for( i=0; i<row; i++)
				{
					for( j=0; j<col; j++) 
					{
						dbMatrixImag[i,j] = 0.0 ;
					}
				}
			}


		}
		

		
		/* ******************************** */
		/* ******************************** */
		/* ******************************** */
		public static void printMatrix(double[,] matrix)  
		{
	
			int i, j, row, col ;
			row = matrix.GetUpperBound(0) - matrix.GetLowerBound(0) + 1;
			col = matrix.GetUpperBound(1) - matrix.GetLowerBound(1) + 1;


			for (i=0; i<row; i++)  
			{
				for (j=0; j<col; j++) 
				{
					
					Console.Write("{0} \t", matrix.GetValue(i,j).ToString() ) ;

				}
				Console.WriteLine() ;
			}

		}

		/* ******************************** */
		public static void printVector(double[] vector)  
		{
			int i, row ;
			row = vector.GetUpperBound(0) - vector.GetLowerBound(0) + 1;

			for (i=0; i<row; i++)  
			{
				Console.Write("{0} \t", vector.GetValue(i).ToString() ) ;
				Console.WriteLine() ;
			}

		}

		/* ******************************** */
		/* ******************************** */
		/* ******************************** */

	} // end class
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -