Is there an easy way to copy part of an array using mex functions?

I'm aware of mxDuplicateArray to copy a whole mxArray, but if I just one to copy just one "column" or "row" of an mxArray, is there a helper mex function for this?
In other words, is there a mex function equivalent to:
B = A(:, i);
C = A(i, :);
etc.

Réponses (2)

James Tursa
James Tursa le 13 Juin 2013
There is no mex API function for this directly, but it would be easy to accomplish a simply copy (e.g., a single column) with a few lines of code. The challenge would be how robust you want the code to be in more general cases. E.g., do you want it to handle all class of variables (not just double)? All possible dimensions? Complex? Sparse? Etc.
If you need help with code for a specific copy (e.g., single row or column) let me know.

5 commentaires

In my case, I just want to copy a real 2D double (column). A bit of help would be most appreciated! I can probably figure it out, but I'm a beginner...so I'm not sure it will be very elegant!
Jan
Jan le 13 Juin 2013
Modifié(e) : Jan le 24 Juin 2013
Do you want the copy as a Matlab or C array? [EDITED, typo: "of" -> "or"]
I'm not sure I understand the question... could you clarify a bit?
A Matlab array is an array, which can be used inside Matlab. It can have different types and has a header, which is used internally in Matlab. A C-array is a pure bolck of reserved memory.
mxArray *MatlabArray;
double *CArray;
MatlabArray = mxCreateDoubleMatrix(2,4, mxREAL);
CArray = (double *) mxMalloc(2 * 4 * sizeof(double));
Ok, now I understand what you were asking (I think there is a typo: "Do you want the copy as a Matlab or C array"). I would prefer the copy as a C array, but still your answer for creating a Matlab array (mxArray) was helpful. I'm still working on my specific implementation though...

Connectez-vous pour commenter.

Jan
Jan le 13 Juin 2013
Modifié(e) : Jan le 19 Juin 2013
To copy a subvector to a new Matlab array:
mxArray *A, *B, *C;
mwSize s1, s2, i;
double *Ap, *Cp;
A = (mxArray *) prhs[0]; // The 1st input, ignore const qualifier
Ap = mxGetPr(A);
s1 = mxGetM(A);
s2 = mxGetN(A);
// B = A(:, i)
B = mxCreateDoubleMatrix(s1, 1, mxREAL);
memcpy(mxGetPr(B), Ap+(i-1)*s1, s1 * sizeof(double)); % EDITED
// C = A(i, :)
C = mxCreateDoubleMatrix(1, s2, mxREAL);
Cp = mxGetPr(C);
for (i = 0; i < s2; i++) {
Cp[i] = Ap[i * s1];
}
This is not tested! Please check this for typos carefully.

8 commentaires

Any comments, Robert?
It seems to be crashing Matlab. When I debugged using DDD, the crash seems to occur at the line:
s1 = mxGetM(A);
Any ideas what could be wrong?
I managed to temporarily get past this problem by hard-coding the matrix sizes into mxCreateDoubleMatrix, but in the first example (B = A(:, i)), only the first column is copied (i.e. B = A(:,1)). I can't figure out how to make this general to copy the i-th column...
i'th column (i is 1-based per the comment):
// B = A(:, i)
B = mxCreateDoubleMatrix(s1, 1, mxREAL);
memcpy(mxGetPr(B), Ap+(i-1)*s1, s1 * sizeof(double));
Thanks, James. I've inserted this in the [EDITED] line. My original line did not contain any dependency to "i" and this was obviously wrong.
Thanks, James! Jan, any ideas on why the mxGetM line is causing Matlab to crash?
mxGetM would crash MATLAB if you passed an invalid pointer to it, or if something upstream corrupted memory and mxGetM just happened to be the point at which the crash finally happened. You will probably need to post your code (not just a code snippet) for us to look at.
A = (mxArray *) prhs[0]; n=mxGetN(A); could fail, when you call the MEX without input arguments, because A is a null pointer then. I cannot test this currently, but as far as I remember mxGetM/N does not test this before.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Produits

Tags

Question posée :

le 13 Juin 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by