Note_20140807_Cprogram_Matlab-for C program_mxCreateNumericArray

This is from C语言中文网,just for my self-learning and sharing with someone.

C语言pow()函数:

头文件:#include <math.h>

pow() 函数用来求 x 的 y 次幂(次方),其原型为:    double pow(double x, double y);

pow()用来计算以x 为底的 y 次方值,然后将结果返回。设返回值为 ret,则 ret = xy

可能导致错误的情况:
  • 如果底数 x 为负数并且指数 y 不是整数,将会导致 domain error 错误。
  • 如果底数 x 和指数 y 都是 0,可能会导致 domain error 错误,也可能没有;这跟库的实现有关。
  • 如果底数 x 是 0,指数 y 是负数,可能会导致 domain error 或 pole error 错误,也可能没有;这跟库的实现有关。
  • 如果返回值 ret 太大或者太小,将会导致 range error 错误。

错误代码:
  • 如果发生 domain error 错误,那么全局变量 errno 将被设置为  EDOM;
  • 如果发生 pole error 或 range error 错误,那么全局变量 errno 将被设置为 ERANGE。
  • 注意,使用 GCC 编译时请加入-lm。


e.g.   #include<stdio.h>
         #include<math.h>
         int main{
             printf(" 7.0^ 3.0 = %d\n",pow(7.0,3.0));

          }

Matlab for C program(from the website of mathswork)


C Functions

mxCreateDoubleMatrix 2-D, double-precision, floating-point array
mxCreateDoubleScalar Scalar, double-precision array initialized to specified value
mxCreateNumericMatrix 2-D numeric matrix
mxCreateNumericArray N-D numeric array
mxCreateString 1-N array initialized to specified string
mxCreateCharMatrixFromStrings 2-D string array initialized to specified value
mxCreateCharArray N-D string array
mxCreateLogicalScalar Scalar, logical array
mxCreateLogicalMatrix 2-D logical array
mxCreateLogicalArray N-D logical array
mxCreateSparseLogicalMatrix 2-D, sparse, logical array
mxCreateSparse 2-D sparse array
mxCreateSparseLogicalMatrix 2-D, sparse, logical array
mxCreateStructMatrix 2-D structure array
mxCreateStructArray N-D structure array
mxCreateCellMatrix 2-D cell array
mxCreateCellArray N-D cell array
mxDestroyArray Free dynamic memory allocated by MXCREATE* functions
mxDuplicateArray Make deep copy of array
mxCalloc Allocate dynamic memory for array, initialized to 0, using MATLAB memory manager
mxMalloc Allocate uninitialized dynamic memory using MATLAB memory manager
mxRealloc Reallocate dynamic memory using MATLAB memory manager
mxFree Free dynamic memory allocated by MXCALLOC, MXMALLOC, or MXREALLOC functions

mxCreateNumericArray (C and Fortran)

C Syntax

#include "matrix.h"
mxArray *mxCreateNumericArray(mwSize ndim, const mwSize *dims, 
         mxClassID classid, mxComplexity ComplexFlag);

Fortran Syntax

mwPointer mxCreateNumericArray(ndim, dims, classid, 
  ComplexFlag)
mwSize ndim
mwSize dims(ndim)
integer*4 classid, ComplexFlag

Arguments

ndim

Number of dimensions. If you specify a value for ndim that is less than 2, mxCreateNumericArray automatically sets the number of dimensions to 2.

dims

Dimensions array. Each element in the dimensions array contains the size of the array in that dimension. For example, in C, setting dims[0] to 5 and dims[1] to 7 establishes a 5-by-7 mxArray. In Fortran, setting dims(1)to 5 and dims(2) to 7 establishes a 5-by-7 mxArray. In most cases, there are ndim elements in the dims array.

classid

Identifier for the class of the array, which determines the way the numerical data is represented in memory. For example, specifying mxINT16_CLASS in C causes each piece of numerical data in the mxArray to be represented as a 16-bit signed integer. In Fortran, use the function mxClassIDFromClassName to derive theclassid value from a MATLAB® class name. See the Description section for more information.

ComplexFlag

If the mxArray you are creating is to contain imaginary data, set ComplexFlag to mxCOMPLEX in C (1 in Fortran). Otherwise, set ComplexFlag to mxREAL in C (0 in Fortran).

Returns

Pointer to the created mxArray, if successful. If unsuccessful in a standalone (non-MEX-file) application, returnsNULL in C (0 in Fortran). If unsuccessful in a MEX-file, the MEX-file terminates and returns control to the MATLAB prompt. The function is unsuccessful when there is not enough free heap space to create the mxArray.

Description

Call mxCreateNumericArray to create an N-dimensional mxArray in which all data elements have the numeric data type specified by classid. After creating the mxArraymxCreateNumericArray initializes all its real data elements to 0. If ComplexFlag equals mxCOMPLEX in C (1 in Fortran), mxCreateNumericArray also initializes all its imaginary data elements to 0. mxCreateNumericArray differs from mxCreateDoubleMatrix as follows:

  • All data elements in mxCreateDoubleMatrix are double-precision, floating-point numbers. The data elements in mxCreateNumericArray can be any numerical type, including different integer precisions.

  • mxCreateDoubleMatrix can create two-dimensional arrays only; mxCreateNumericArray can create arrays of two or more dimensions.

mxCreateNumericArray allocates dynamic memory to store the created mxArray. When you finish with the created mxArray, call mxDestroyArray to deallocate its memory.

MATLAB automatically removes any trailing singleton dimensions specified in the dims argument. For example, ifndim equals 5 and dims equals [4 1 7 1 1], the resulting array has the dimensions 4-by-1-by-7.

The following table shows the C classid values and the Fortran data types that are equivalent to MATLAB classes.

MATLAB Class Name

classid Value

Fortran Type

int8

mxINT8_CLASS

BYTE

uint8

mxUINT8_CLASS

 

int16

mxINT16_CLASS

INTEGER*2

uint16

mxUINT16_CLASS

 

int32

mxINT32_CLASS

INTEGER*4

uint32

mxUINT32_CLASS

 

int64

mxINT64_CLASS

INTEGER*8

uint64

mxUINT64_CLASS

 

single

mxSINGLE_CLASS

REAL*4

double

mxDOUBLE_CLASS

REAL*8

single, with imaginary components

mxSINGLE_CLASS

COMPLEX*8

double, with imaginary components

mxDOUBLE_CLASS

COMPLEX*16

Examples

See the following examples in matlabroot/extern/examples/refbook.

See the following examples in matlabroot/extern/examples/mx.




mxGetPr (C and Fortran)


C Syntax

#include "matrix.h"
double *mxGetPr(const mxArray *pm);

Fortran Syntax

mwPointer mxGetPr(pm)
mwPointer pm

Arguments

pm

Pointer to an mxArray of type double

Returns

Pointer to the first element of the real data. Returns NULL in C (0 in Fortran) if there is no real data.

Description

Use mxGetPr on arrays of type double only. Use mxIsDouble to validate the mxArray type. For other mxArraytypes, use mxGetData.

Call mxGetPr to access the real data in the mxArray that pm points to. Once you have the starting address, you can access any other element in the mxArray.



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值