From: Dan on
MATLAB does not officially support or endorse any in-place operations in mex routines. That being said, if you know what you are doing then you can indeed do it and avoid data copies. However, if the variable is being shared with another variable in the MATLAB workspace, then you will change *both* variables ... that is why you have to know what you are doing so you can avoid this undesirable side effect. For your code snippet above, you would not use mxSetPr. You would just do this:
>
> B = mxGetPr(prhs[0]);
> I = mxGetPr(prhs[1]);
> / operation on array B and I involving elements change /
>
Then you are done. Since B and I are pointing *directly* at the data area of the original variable in the MATLAB workspace, you will immediately change the contents of the MATLAB workspace variable when you do this. There is nothing to set as far as the plhs array is concerned. i.e., don't do *anything* with the plhs array. There is no need to set any plhs since you are not returning anything back to MATLAB as far as a function output is concerned ... you are simply modifying the data contents of an existing variable in-place. In fact, what I would do is generate an error (call mexErrMsgTxt) if nlhs > 0 in this case.
>
>
> James Tursa

Thanks, James. It works as you described without much problem. I have already seen 10 times speed up.
First  |  Prev  | 
Pages: 1 2 3
Prev: Vehicle Detection
Next: Optimization of a loop