Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MAT

ID: 3866344 • Letter: M

Question

MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLAB MATLB MATLAB MATLAB

Write a MATLAB function filter_negative that takes one parameter a 1-d array and returns a 1-d array. This function should return an array containing all non-negative items in the input array, in the same order.

Be sure to suppress all output. Think about how to append things to an array.

For example:

Test Result disp(filter_negative([-1,-1,2.0]));      2 Write a MATLAB function filter_negative that takes one parameter a 1-d array and returns a 1-d array. This function should return an array containing all non-negative items in the input array, in the same order. Be sure to suppress all output. Think about how to append things to an array. For example: Test Result disp(filter_negative(-1,-1,2.0]));

Explanation / Answer

#define ARRAY_IN prhs[0] #define VECTOR_IN prhs[1] #define VECTOR_OUT plhs[0] void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double twoDarray[2][2];/* 2 dimensional C array to pass to workFcn() */ int row,col; /* loop indices */ int m,n; /* temporary array size holders */ /* Step 1: Error Checking Step 1a: is nlhs 1? If not, generate an error message and exit mexample (mexErrMsgTxt does this for us!) */ if (nlhs!=1) mexErrMsgTxt("mexample requires one output argument."); /* Step 1b: is nrhs 2? */ if (nrhs!=2) mexErrMsgTxt("mexample requires two input arguments, ARRAY_IN and VECTOR_IN."); /* Step 1c: Is ARRAY_IN a 2x2 numeric (nonstring), full (non- sparse), real (noncomplex) matrix? */ if (mxGetM(ARRAY_IN)!=2 || mxGetN(ARRAY_IN)!=2 || mxIsChar(ARRAY_IN) || mxIsSparse(ARRAY_IN) || mxIsComplex(ARRAY_IN)) mexErrMsgTxt("First argument to mexample must be a 2x2, full, numeric, real-valued array."); /* Step 1d: Is VECTOR_IN a 2x1 or 1x2 numeric, full, real- valuedvector? */ m=mxGetM(VECTOR_IN); /* Assigning result of mxGetM and mxGetN to variables */ n=mxGetN(VECTOR_IN); /* with simpler names makes for easier code reading. */ if (!((m==2 && n==1) || (m==1 && n==2)) || mxIsChar(VECTOR_IN) || mxIsSparse(VECTOR_IN) || mxIsComplex(VECTOR_IN)) mexErrMsgTxt("Second argument to mexample must be 2 element, full, numeric, real-valued vector."); /* Step 2: Allocate memory for return argument(s) */ VECTOR_OUT = mxCreateDoubleMatrix(2, 1, mxREAL); /* create a 2x1 full, numeric, real-valued array */ /* Step 3: Convert ARRAY_IN to a 2x2 C array MATLAB stores a two-dimensional matrix in memory as a one- dimensional array. If the matrix is size MxN, then the first M elements of the one-dimensional array correspond to the first column of the matrix, and the next M elements correspond to the second column, etc. The following loop converts from MATLAB format to C format: */ for (col=0; col