WeeBeng
December 17th, 2002, 11:44 PM
This is more of a C type of question.I need to copy the memory of a 2-dimensional array into another target buffer. Usually this is what I do if the 2-dimensional array is small.
double testmatrix1[Numtimepts][Numtimepts];
int I,J;
for(I=0;I<Numtimepts;I++){
for(J=0;J<Numtimepts;J++){
testmatrix1[I][J]=(double)rand()/RAND_MAX*2.1;
}
}
memcpy((void *)mxGetPr(mat1), (void *)testmatrix1, sizeof(testmatrix1));
where mxGetPr(mat1) is a function of an external programme (Matlab) I am linking this C programme to and gives a pointer to the memory buffer I need.
However for an unknown 2-dimensional array, I usually use pointer to a pointer .
double **matrix1;
for(I=0;I<Numtimepts;I++)
matrix1 = malloc(Numtimepts*sizeof(double *));
for(I=0;I<Numtimepts;I++)
matrix1[I]=malloc(Numtimepts*sizeof(double *));
for(I=0;I<Numtimepts;I++){
for(J=0;J<Numtimepts;J++){
matrix1[I][J]=(double)rand()/RAND_MAX*2.1;
}
}
memcpy((void *)mxGetPr(mat1), (void *)matrix1[0], Numtimepts*Numtimepts*sizeof(double));
I tried the above statement but it failed to copy the correct memory buffer.
This statement:
memcpy((void *)mxGetPr(mat1), (void *)matrix1[0], Numtimepts*sizeof(double));
just allows me to copy the first row of the matrix1 which is understandable. So how do I copy the rest of the rows of the matrix1 into the target memory row by row?
The following statement
for (I=0;I<Numtimepts;I++)
memcpy((void *)mxGetPr(mat1), (void *)(matrix1[I]+Numtimepts*I*sizeof(double)), Numtimepts*sizeof(double));
also fails to acheive what I want.
Finally, due to the way Matlab declares its arrays, ie column by column instead of row by row like C, I will end up with a transpose matrix, is there any way to copy my memory column by column to the memory buffer?
I understand that Cpp may deal with the problem more efficiently but as I am still catching up on Cpp , this current solution will serve my needs for the time being.
Thanx a lot for any help.
double testmatrix1[Numtimepts][Numtimepts];
int I,J;
for(I=0;I<Numtimepts;I++){
for(J=0;J<Numtimepts;J++){
testmatrix1[I][J]=(double)rand()/RAND_MAX*2.1;
}
}
memcpy((void *)mxGetPr(mat1), (void *)testmatrix1, sizeof(testmatrix1));
where mxGetPr(mat1) is a function of an external programme (Matlab) I am linking this C programme to and gives a pointer to the memory buffer I need.
However for an unknown 2-dimensional array, I usually use pointer to a pointer .
double **matrix1;
for(I=0;I<Numtimepts;I++)
matrix1 = malloc(Numtimepts*sizeof(double *));
for(I=0;I<Numtimepts;I++)
matrix1[I]=malloc(Numtimepts*sizeof(double *));
for(I=0;I<Numtimepts;I++){
for(J=0;J<Numtimepts;J++){
matrix1[I][J]=(double)rand()/RAND_MAX*2.1;
}
}
memcpy((void *)mxGetPr(mat1), (void *)matrix1[0], Numtimepts*Numtimepts*sizeof(double));
I tried the above statement but it failed to copy the correct memory buffer.
This statement:
memcpy((void *)mxGetPr(mat1), (void *)matrix1[0], Numtimepts*sizeof(double));
just allows me to copy the first row of the matrix1 which is understandable. So how do I copy the rest of the rows of the matrix1 into the target memory row by row?
The following statement
for (I=0;I<Numtimepts;I++)
memcpy((void *)mxGetPr(mat1), (void *)(matrix1[I]+Numtimepts*I*sizeof(double)), Numtimepts*sizeof(double));
also fails to acheive what I want.
Finally, due to the way Matlab declares its arrays, ie column by column instead of row by row like C, I will end up with a transpose matrix, is there any way to copy my memory column by column to the memory buffer?
I understand that Cpp may deal with the problem more efficiently but as I am still catching up on Cpp , this current solution will serve my needs for the time being.
Thanx a lot for any help.