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

📄 例11-5.m

📁 这是一个MATLAB7.0基础与提高例题的所有源码
💻 M
字号:
#include "mex.h"
/* 计算功能子程序 */
void convec(double *xr, double *xi, int nx, double *yr, 
            double *yi, int ny, double *zr, double *zi)
{
  int i,j;
  
  zr[0] = 0.0;
  zi[0] = 0.0;
  /* 计算复数矩阵的卷积 */
  for (i = 0; i < nx; i++) {
    for (j = 0; j < ny; j++) {
      *(zr+i+j) = *(zr+i+j) + *(xr+i) * *(yr+j) - *(xi+i) 
          * *(yi+j);
      *(zi+i+j) = *(zi+i+j) + *(xr+i) * *(yi+j) + *(xi+i) 
          * *(yr+j);
    }
  }
}
/* 入口子程序 */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
  double  *xr, *xi, *yr, *yi, *zr, *zi;
  int     rows, cols, nx, ny;
    
  /*  检查输入参数的个数 */
  if (nrhs != 2)
    mexErrMsgTxt("Two inputs required.");
  if (nlhs > 1)
    mexErrMsgTxt("Too many output arguments.");

  /*  检查输入参数是否为矩阵 */
  if (mxGetM(prhs[0]) != 1 || mxGetM(prhs[1]) != 1)
    mexErrMsgTxt("Both inputs must be row vectors.");
  rows = 1; 

  /* 检查输入的参数是否为复数 */
  if (!mxIsComplex(prhs[0]) || !mxIsComplex(prhs[1]))
    mexErrMsgTxt("Inputs must be complex.\n");
  
  /* 获得每个输入向量的长度 */
  nx = mxGetN(prhs[0]);
  ny = mxGetN(prhs[1]);

  /* 获得指向输入参数实部和虚部的指针 */
  xr = mxGetPr(prhs[0]);
  xi = mxGetPi(prhs[0]);
  yr = mxGetPr(prhs[1]);
  yi = mxGetPi(prhs[1]);
  
  /* 创建新的数组并设置输出参数指针 */
  cols = nx + ny - 1;
  plhs[0] = mxCreateDoubleMatrix(rows, cols, mxCOMPLEX);
  zr = mxGetPr(plhs[0]);
  zi = mxGetPi(plhs[0]);

  /* 调用子程序 */
  convec(xr, xi, nx, yr, yi, ny, zr, zi);

  return;
}

⌨️ 快捷键说明

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