June 26th, 2013, 08:33 AM

Meshgrid from Matlab to C/c++
Hi,
translating this code from Matlab to C. Code of Matlab:
Code:
x = (cols/2 : (cols/2  1))/(cols/2);
y = (rows/2 : (rows/2  1))/(rows/2);
[x,y] = meshgrid(x,y);
What I did in C/C++ is :
Code:
double x[cols],y[rows];
double X[cols][rows], Y[cols][rows];
double epsilon=0.0001;
for(int j=0;j<cols;j++){
for(int i=(cols/2);i<=(cols/2)1;i++){
x[j]= i/(cols/2);
}
}
for(int k=0;k<rows;k++){
for(int z=(rows/2);z<=(rows/2)1;z++){
y[k]= z/(rows/2);
}
}
for(int i=0;i<cols;i++){
for(int z=0;z<rows;z++){
X[i][z]=x[i];
Y[i][z]=y[z];
}
}
Is it right?
June 26th, 2013, 11:17 AM

Trying printing first elements of X (X[0][0],X[1][0]...)
I have 0 ever. SO it s wrong. Someone know why?
X[0][0] must be 1
June 26th, 2013, 11:25 AM

I suggest you show the smallest possible complete program that illustrates your problem.
And also state the language you're actually using along with the standard version if your using C.
Jim
June 26th, 2013, 11:37 AM

I tried to print first element of this part:
Code:
for(int j=0;j<cols;j++){
for(int i=(cols/2);i<=(cols/2)1;i++){
x[j]= i/(cols/2);
}
}
for(int e=0;e<3;e++){
printf("element:%f\n",x[e]);
}
It print 0 for all the elements. Of course (cols/2)/cols/2 must be 1. I m using Eclipse with C/C++ project and C99
June 26th, 2013, 12:10 PM

It print 0 for all the elements.
What do you expect the output to be?
As I already said post the smallest possible complete program that illustrates your problem. The snippets you have shown so far are not enough to see what you are doing correctly and incorrectly.
Jim
June 26th, 2013, 12:27 PM

What I' m doing is Precompute some stuff to speed up filter construction. I' m working with images and so OpenCV. Cols and rows are rows and cols of an image. I mean for example an image of 534x696. With this code I wrote it' s to write after a matrix values contain radius from center. So when I divide for example first element :
534/2 / 534/2 , I must have 1 in radius matrix.
The second must be (533/2)/534/2 etc...
Last edited by residentelvio; June 26th, 2013 at 12:35 PM.
June 26th, 2013, 12:56 PM

There is something wrong with 2nd for, because the first one works good. If I want to print with the option x[j]=0. It prints 0,1,2. If I print x[j]=i, it prints ever 347. That's mean + doesn' t go forward. Can it be because of minus?
Last edited by residentelvio; June 26th, 2013 at 01:00 PM.
June 26th, 2013, 07:00 PM

Originally Posted by residentelvio
I tried to print first element of this part:
Code:
for(int j=0;j<cols;j++){
for(int i=(cols/2);i<=(cols/2)1;i++){
x[j]= i/(cols/2);
}
}
for(int e=0;e<3;e++){
printf("element:%f\n",x[e]);
}
It print 0 for all the elements. Of course (cols/2)/cols/2 must be 1. I m using Eclipse with C/C++ project and C99
You needs paper and pencil to find out.
Code:
Assumed cols = 3
j j<? i i<=? x[j] next i nextj
0 3 3/2=1 (3/2)1=0 1/(3/2)=1 0
0 0 0/(3/2)=0 1
1 0 exit iloop 1
1 3 1 0 1/(3/2)=1 0
0 0 0/(3/2)=0 1
1 0 exit iloop 2
2 3 1 0 1/(3/2)=1 0
0 0 0/(3/2)=0 1
1 0 exit iloop 3
3 3 exit jloop
result: x[0] = 0
x[1] = 0
x[2] = 0
even if cols >100, you 'll get only 0, because i become 0 in every loop.
You needs paper and pencil.
June 27th, 2013, 12:16 AM

You should describe what does x(a:b) mean.
If not, I could be a bad mind reader. ;)
I guess x(a:b) means x has value between a and b.
Since cols and rows mean width and height of an image.
I can assign
Code:
double x_a = (double)cols/2;
double x_b = ((double)cols/2  1)/((double)cols/2);
double y_a = (double)rows/2;
double y_b = ((double)rows/2  1)/((double)rows/2);
What I need to know is how many steps you will trace through x and y direction?
I guess (again) m and n steps. I say ...
Code:
int m = 5;
int n = 4;
Then I need to make precalculation value to increase in m and n steps.
Just say....
Code:
double m_inc = (x_bx_a) / m;
double n_inc = (y_by_a) / n;
Initial values in both directions...
Code:
double x_0 = x_a;
double y_0 = y_a;
Iterating through all grids...
Code:
...
double X[10][10]; //X[n+1][m+1]
double Y[10][10]; //Y[n+1][m+1]
...
for(i = 0 ; i<=n; i++){
for(j= 0; j<=m; j++) {
X[i][j] = x_0 + m_inc*j;
Y[i][j] = y_0 + n_inc*i;
}
}
Let it print out the results.
Code:
printf("\nX:\n");
for(i=0 ; i<=n; i++){
for(j=0; j<=m; j++) {
printf("%6.2lf ", X[i][j]);
}
printf("\n");
}
printf("\nY:\n");
for(i=0 ; i<=n; i++){
for(j=0; j<=m; j++) {
printf("%6.2lf ", Y[i][j]);
}
printf("\n");
}
You need paper and pencil.
June 27th, 2013, 10:11 AM

Ok so I try to explain better. I m not very good in English. So i' ll try better:
x is a monodimensional array of lenght=cols.
y x is a monodimensional array of lenght=rows.
Cols and rows depends from the picture I charge in the project. (EX cols=696,rows=534).
x[cols]= all the elements from (cols/2)/(cols/2) to (cols/2)1/(cols/2)
(example) > x[0] = (696/2)/(696/2)= 1;
x[1]= (695/2)/(696/2)= .0,99
....
x[695]= 0,99
For y it s the same. After done this I have to meshgrid. I mean:
transform the domain specified by vectors x and y into arrays X and Y , that are bidimensional arrays.
The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.
June 27th, 2013, 11:32 AM

June 27th, 2013, 03:54 PM

x is a monodimensional array of lenght=cols.
y x is a monodimensional array of lenght=rows.
Cols and rows depends from the picture I charge in the project. (EX cols=696,rows=534).
I hope you understand that the size of arrays in C (except the C99 standard) and C++ must be compile time constants. If you want do be able to use run time sizes you must use dynamic memory in C/C++ or vectors in C++.
Jim
June 28th, 2013, 01:26 AM

O.K. It's good point to continue.
Write your expected out put on a paper, you will find an algorithm easily.
You wrote
Originally Posted by residentelvio
Cols and rows depends from the picture I charge in the project. (EX cols=696,rows=534).
x[cols]= all the elements from (cols/2)/(cols/2) to (cols/2)1/(cols/2)
(example) >
x[0] = (696/2)/(696/2)= 1;
x[1]= (695/2)/(696/2)= .0,99
....
x[695]= 0,99
From this, you can generalize it to
Code:
x[i] = (i696)/2 / (696/2)
where i = 0, 1, .... , 696*2.
Mention that the last index is not 695. It is 696*2 = 1,393 (cols = 696, 965, ..., 0, ..., 695).
Just say now:
Code:
int cols = 696;
int max_cols = cols*2; // size of dynamic array
double *x = (double *) malloc (max_cols * sizeof(double));
/* double *x = new double [max_cols]; // C++ */
for(int i = 0; i < max_cols; i++) { //i=0..1392
x[i] = (double)(imax_cols)/2 / ((double)max_cols/2);
//cast to double before division!
}
Repeat the same algorithm for y[].
June 28th, 2013, 04:58 AM

Ok I solved it because of you. I didn t use malloc because I work with C99. Now to do the meshgrid :
Code:
double X[cols*2][rows*2];
double Y[cols*2][rows*2];
for(int m=0;m<cols*2;m++){
for(int n=0;n<rows*2;n++{
X[m][n]=x[m];
Y[m][n]=y[n];
}
}
Is it right?
Last edited by residentelvio; June 28th, 2013 at 05:02 AM.
June 29th, 2013, 02:17 AM

I don't think so.
double X[cols*2][rows*2] is not correct. Compiler needs exact size for static array. Using dynamic array should be better way.
Code:
double **X = new (double*) [max_rows];
for(i=0; i<max_rows; i++)
X[i] = new double [max_cols];
Assignment
Code:
X[m][n] = x[n] ; // cols index
I don't understand why you do that. X matrix eat 16 MB, very huge. I'm curios, how long does computer take for calculation.