00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mex.h"
00020
00021 void
00022 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00023 {
00024 mwSize nsubs, x;
00025 mwIndex index;
00026 double *temp;
00027 mwIndex *subs;
00028
00029
00030 if (nrhs != 2) {
00031 mexErrMsgTxt("Two input arguments required.");
00032 }
00033 if (nlhs > 1) {
00034 mexErrMsgTxt("Too many output arguments.");
00035 }
00036
00037
00038 if (!mxIsDouble(prhs[0])) {
00039 mexErrMsgTxt("First input argument must be a double.");
00040 }
00041
00042 if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1])) {
00043 mexErrMsgTxt("Second input argument must be a real double.");
00044 }
00045
00046 nsubs=mxGetNumberOfDimensions(prhs[0]);
00047
00048
00049 if (mxGetNumberOfElements(prhs[1]) != nsubs){
00050 mexErrMsgTxt("You must specify an index for each dimension.");
00051 }
00052
00053
00054 subs=mxCalloc(nsubs,sizeof(mwIndex));
00055
00056
00057
00058
00059
00060
00061 temp=mxGetPr(prhs[1]);
00062
00063 for (x=0;x<nsubs;x++){
00064 subs[x]=(mwIndex)temp[x]-1;
00065 if (temp[x]> ((mxGetDimensions(prhs[0]))[x]) ){
00066 mxFree(subs);
00067 mexErrMsgTxt("You indexed above the size of the array.");
00068 }
00069 }
00070
00071
00072
00073 index = mxCalcSingleSubscript(prhs[0], nsubs, subs);
00074
00075
00076 plhs[0] = mxCreateDoubleMatrix(1, 1, mxIsComplex(prhs[0]) ? mxCOMPLEX : mxREAL);
00077
00078
00079 mxFree(subs);
00080
00081
00082 mxGetPr(plhs[0])[0]= mxGetPr(prhs[0])[index];
00083 if (mxIsComplex(prhs[0])) {
00084 mxGetPi(plhs[0])[0]= mxGetPi(prhs[0])[index];
00085 }
00086 }