Need help initialising a 2D array

  • Thread starter pamparana
  • Start date
  • Tags
    2d Array
In summary, to efficiently set up a 2D array 'phi' in Matlab with a gradient that is an identity mapping in terms of the spatial coordinate, you can use the meshgrid function to initialize x and y vectors and then plot z as a function of x and y. This can be done by using the equation phi(x, y) = 0.5 * (x^2 + y^2).
  • #1
pamparana
128
0
Hello everyone,

I have a newbie Matlab question. I need to set up a 2D array 'phi' whose gradient is an identity mapping in terms of the spatial coordinate.

So, basically I have a 2D image and what I want to do is set up phi at each pixel (x, y) as follows:

phi(x, y) = 0.5 * (x^2 + y^2), so that grad(phi(x, y)) = (x, y).

My question is how can I set this up in Matlab in an efficient way. So, given a 2D image, how can I set up this phi using the equation phi(x, y) = 0.5 * (x^2 + y^2).

Any help would be greatly appreciated.

Many thanks,

Luca
 
Physics news on Phys.org
  • #2
Last edited by a moderator:

Related to Need help initialising a 2D array

1. How do I declare and initialise a 2D array in Java?

To declare and initialise a 2D array in Java, you can use the following syntax:

data_type[][] array_name = new data_type[row_size][column_size];

For example, to create a 2D array of integers with 3 rows and 4 columns, you can use:

int[][] numbers = new int[3][4];

2. Can I declare and initialise a 2D array with different data types?

Yes, you can declare and initialise a 2D array with different data types in Java. However, all elements in the 2D array must be of the same data type. For example, you can have a 2D array of type int, but not a 2D array with some elements of type int and some elements of type String.

3. How do I initialise a 2D array with specific values?

To initialise a 2D array with specific values, you can use the following syntax:

data_type[][] array_name = {{value1, value2, ...}, {value3, value4, ...}, ...};

For example, to create a 2D array of integers with the values 1, 2, 3 in the first row and 4, 5, 6 in the second row, you can use:

int[][] numbers = {{1, 2, 3}, {4, 5, 6}};

4. How do I initialise a 2D array with a variable number of rows and columns?

To initialise a 2D array with a variable number of rows and columns, you can use the following syntax:

data_type[][] array_name = new data_type[rows][];array_name[0] = new data_type[column_size1];array_name[1] = new data_type[column_size2];...array_name[rows-1] = new data_type[column_sizeN];

For example, to create a 2D array of integers with 3 rows and a different number of columns for each row, you can use:

int[][] numbers = new int[3][];numbers[0] = new int[2];numbers[1] = new int[3];numbers[2] = new int[4];

5. Can I initialise a 2D array with a nested for loop?

Yes, you can initialise a 2D array with a nested for loop in Java. This can be useful when you want to set specific values for each element in the array. Here is an example of how this can be achieved:

int[][] numbers = new int[3][4];for(int i = 0; i < numbers.length; i++) { for(int j = 0; j < numbers[i].length; j++) { numbers[i][j] = i+j; }}

This will create a 2D array of integers with 3 rows and 4 columns, and each element will be set to the sum of its row and column index.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • General Math
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
424
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
14
Views
1K
  • Introductory Physics Homework Help
2
Replies
64
Views
2K
  • Calculus and Beyond Homework Help
Replies
13
Views
311
  • Differential Equations
Replies
9
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
843
Back
Top