Moving Double pointer in Mx-array

  • Follow


Hello, 

I have got a problem while trying to do Pointer-arithmetics in a mxDouble matrix. 
The input I used for my Mex file was a two-dimensional array a=randn(100,5). 

In the Mex-function body I tried to access this input matrix by creating a pointer: 
double* data = mxGetPr(prhs[0]);
and 
plhs[0] = mxCreateDoubleMatrix(100, 5, mxREAL);
double* output = mxGetPr(plhs[0]);

For the next step of my mex-file, I am required to pass the data columnwise as a double* into one function: 

void function1(double* data, double* output){
                   // multiply each value by one factor
}

My problem is that I am not successful in incrementing the two pointers "data and output", in order to pass each column successively into function 1. 

If I try writing a for-loop 

for (int i=0; i< 5;i++){
       data=data+(i*100*sizeof(double));
       output=output+(i*100*sizeof(double));
       function1(data,output);
}

This will only work for the first column. The latter for columns remain always zeros. 
What did I get wrong by assuming that memory in Mx-arrays is laid out in format 
lines x columns? 

Thanks very much for your help!! 
0
Reply Martin 12/18/2009 10:40:21 PM

I'm a bit new to this myself but I believe that you don't need the sizeof part.  The compiler realizes it is a double and takes care of address increments that are appropriate for the size of the type.  My guess is that if you were to make your array a bit larger you would see your 2nd column in column 9, because the sizeof a double is 8.  I'm a bit surprised your program doesn't crash as I would think you are writing past the memory of the variable, but again, I'm a bit new to this ...

"Martin " <martin.westerberg@stcatz.ox.ac.uk> wrote in message <hgh0cl$sor$1@fred.mathworks.com>...
> Hello, 
> 
> I have got a problem while trying to do Pointer-arithmetics in a mxDouble matrix. 
> The input I used for my Mex file was a two-dimensional array a=randn(100,5). 
> 
> In the Mex-function body I tried to access this input matrix by creating a pointer: 
> double* data = mxGetPr(prhs[0]);
> and 
> plhs[0] = mxCreateDoubleMatrix(100, 5, mxREAL);
> double* output = mxGetPr(plhs[0]);
> 
> For the next step of my mex-file, I am required to pass the data columnwise as a double* into one function: 
> 
> void function1(double* data, double* output){
>                    // multiply each value by one factor
> }
> 
> My problem is that I am not successful in incrementing the two pointers "data and output", in order to pass each column successively into function 1. 
> 
> If I try writing a for-loop 
> 
> for (int i=0; i< 5;i++){
>        data=data+(i*100*sizeof(double));
>        output=output+(i*100*sizeof(double));
>        function1(data,output);
> }
> 
> This will only work for the first column. The latter for columns remain always zeros. 
> What did I get wrong by assuming that memory in Mx-arrays is laid out in format 
> lines x columns? 
> 
> Thanks very much for your help!! 
0
Reply Jim 12/19/2009 1:02:06 AM


"Martin " <martin.westerberg@stcatz.ox.ac.uk> wrote in message <hgh0cl$sor$1@fred.mathworks.com>...
> 
> for (int i=0; i< 5;i++){
>        data=data+(i*100*sizeof(double));
>        output=output+(i*100*sizeof(double));
>        function1(data,output);
> }

for( int i=0; i< 5; i++ ) {
    function1(data,output);
    data += 100;
    output += 100;
 }

James Tursa
0
Reply James 12/19/2009 3:21:02 AM

Thank you very much James and Jim, it works now? 

There is one more question I am asking myself: 
If the input-matrix was just a line-vector of type 1 x n, would I get the same behavior to retrieve successive values with the actions 

- double *pointer++
- pointer[n]

as if the input vector was a column-vector of type m x 1? 

Is it correct that in that case it won't make a difference to move the pointer "horizontally" or "vertically"? But as soon as it comes to 2-dimensional matrices, the order will become column-wise. 

Thanks very much, 
Martin
0
Reply Martin 12/19/2009 8:59:05 AM

Dear Martin!

> There is one more question I am asking myself: 
> If the input-matrix was just a line-vector of type 1 x n, would I get the same behavior to retrieve successive values with the actions 
> 
> - double *pointer++
> - pointer[n]
> 
> as if the input vector was a column-vector of type m x 1? 
> 
> Is it correct that in that case it won't make a difference to move the pointer "horizontally" or "vertically"? But as soon as it comes to 2-dimensional matrices, the order will become column-wise. 

Correct!
Even for 3D arrays the first dimension is concerned at first, then the second, then the third. The indexing equals the linear indexing inside Matlab, see SUB2IND and IND2SUB.
So you can test an algorithm inside Matlab also, example:
  M = magic(4);
  for i = 1:numel(M)
    disp(M(i));
  end

Good luck, Jan
0
Reply Jan 12/19/2009 10:23:02 AM

4 Replies
237 Views

(page loaded in 0.045 seconds)

Similiar Articles:













7/27/2012 8:07:18 AM


Reply: