Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 26th, 2013, 09:33 AM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
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?

#2
June 26th, 2013, 12:17 PM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
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

#3
June 26th, 2013, 12:25 PM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 151
Time spent in forums: 1 Week 6 h 29 m 25 sec
Reputation Power: 18
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

#4
June 26th, 2013, 12:37 PM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
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

#5
June 26th, 2013, 01:10 PM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 151
Time spent in forums: 1 Week 6 h 29 m 25 sec
Reputation Power: 18
Quote:
 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

#6
June 26th, 2013, 01:27 PM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
What I' m doing is Pre-compute 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 01:35 PM.

#7
June 26th, 2013, 01:56 PM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
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 02:00 PM.

#8
June 26th, 2013, 08:00 PM
 Homi@th
Contributing User

Join Date: Jun 2013
Posts: 40
Time spent in forums: 1 Day 8 h 58 m 5 sec
Reputation Power: 18
Quote:
 Originally Posted by residentelvio I tried to print first element of this part: Code: ```for(int j=0;j

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 i-loop                                                               1
1      3             -1                0                    1/(3/2)=1                  0
0                 0                    0/(3/2)=0                 1
1                 0    exit i-loop                                                               2
2      3            -1                 0                    1/(3/2)=1                  0
0                0                     0/(3/2)=0                 1
1                0    exit i-loop                                                               3
3     3 exit j-loop

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.

#9
June 27th, 2013, 01:16 AM
 Homi@th
Contributing User

Join Date: Jun 2013
Posts: 40
Time spent in forums: 1 Day 8 h 58 m 5 sec
Reputation Power: 18
You should describe what does x(a:b) mean.

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 pre-calculation value to increase in m and n steps.
Just say....
Code:
```
double m_inc = (x_b-x_a) / m;
double n_inc = (y_b-y_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.

#10
June 27th, 2013, 11:11 AM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
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.

#11
June 27th, 2013, 12:32 PM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
it seems doesn t work

#12
June 27th, 2013, 04:54 PM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 151
Time spent in forums: 1 Week 6 h 29 m 25 sec
Reputation Power: 18
Quote:
 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

#13
June 28th, 2013, 02:26 AM
 Homi@th
Contributing User

Join Date: Jun 2013
Posts: 40
Time spent in forums: 1 Day 8 h 58 m 5 sec
Reputation Power: 18
O.K. It's good point to continue.
Write your expected out put on a paper, you will find an algorithm easily.

You wrote
Quote:
 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] = (i-696)/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)(i-max_cols)/2 / ((double)max_cols/2);
//cast to double before division!
}```

Repeat the same algorithm for y[].

#14
June 28th, 2013, 05:58 AM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation Power: 0
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 06:02 AM.

#15
June 29th, 2013, 03:17 AM
 Homi@th
Contributing User

Join Date: Jun 2013
Posts: 40
Time spent in forums: 1 Day 8 h 58 m 5 sec
Reputation Power: 18
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Meshgrid from Matlab to C/c++