Question

2. Neighbor Identification. MATLAB languageN = 6 1 5 9 13 17 21 2 6 10 14 18 22 M = 4 3 7 11 15 19 23 4 8 12 16 20 24
Many engineering problems can be solved numerically by dividing a large, compli- cated geometry into a multitude of smaller easier-to-solve cells. The quantities rep- resented in an individual cell (for example, temperature, velocity, and/or pressure) depend only on the values of those quantities stored at the cell’s nearest neighbors. In this problem, we will write a script to identify all the neighbors of a given cell in a rectangular array. Consider the numbered setup shown below.

The neighbors of cells 4 and 18 identified on a linearly-indexed grid with M = 4 and N = 6. Two different sets of neighbors have been identified in the figure above: (1) cell 4 has neighbors 3, 7, 8, and (2) cell 18 has neighbors 13, 14, 15, 17, 19, 21, 22, and 23. This configuration of cell s uses the concept of linear indexing, in which a single number (as opposed to two, e.g. (x, y)) is used to represent a cell’s location in a 2D grid. In this problem, we will assume that cell numbering always starts in the upper-left corner, proceeds down the first column, then down the second column, etc., as shown.

  1. (a) Begin by asking the user to input three separate values representing the num- ber of rows in the array, M, the number of columns, N, and the cell for which we will be identifying neighbors, P. Your code must produce an error if eitherM or N is less than 2 or a non-integer value. Similarly your code must produce an error if the value of P is a non-integer or does not fall in the range of valid cellnumber,1≤P ≤(M×N).

  2. (b) Once the values of M, N, and P have been collected and validated, we can begin identifying all the neighbors of P . You may find it helpful to write down in your pseudo-code the numerical rules and patterns that let us classify a cell, P , as being located on the walls (left, right, top, bottom), or corners (top left, top right, bottom left, bottom right). Note that the maximum number of neighbors is

2

8 (4 orthogonal + 4 diagonal), while the ones located on the walls each has 5 neighbors, and the ones located in the corners each has 3 neighbors.

(c) Print the neighbors of the user-input cell ID, P, to the command window. Use the formate shown below.

Cell ID: 4

Neighbors: 3 7 8

The neighbors needs to be in ascending order. Keep your logic as concise and organized as possible.

(d) Can a linear indexing scheme be used for a 3D grid? If so, explain the number- ing convention you would employ, using examples to explain your convention if necessary. In 2D, we can classify every cell as either: (1) corner, (2) edge, or (3) interior. Extend this classification system for a 3D grid and define how many neighbors surround each of these different cell types.

Note: Your code should work for any valid values of M and N, do not rely on hard- codedlogicaltestthatpresupposeaparticularvalueforMandN,e.g.if P == 24to catch the bottom-right corner in the grid above. Keep everything defined in terms of M and N, and/or other derived quantities.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

  • You can find the answers for question a-c in the code below.
    • Answer d: Yes we use linear indexing in 3-D. Here, Cell numbering always starts from the first 2-D grid (i.e. wall) in the upper-left corner, proceeds down the first column, then down the second column continuing to cover complete wall and then the 2nd grid and till the last wall. We can classify every cell as either: (1) corner, (2) edge, (3) Surface, or (4) interior. Having, 7, 11, 17, and 26 neighbors respectively.
  • I have provided the properly commented code in both text and image format so you can easily copy the code as well as check for correct indentation.
  • I have provided the output image of the code so you can easily cross-check for the correct output of the code.
  • Have a nice and healthy day!!

CODE TEXT

clc;

clear;

% Prompting user to enter M, N and P and converting it to integer

M=int32(input('Enter Number of rows: '));

N=int32(input('Enter Number of columns: '));

P=int32(input('Enter Cell Number: '));

% Checking for integer and P limit

if ~isinteger(M) || ~isinteger(N) || ~isinteger(P)

fprintf('%d %d %d',isinteger(M),isinteger(N),isinteger(P))

disp('Please enter integer only in all feilds');

elseif M<2 || N<2

disp('Please enter value of row and column >= 2');

elseif P<=0 || P>(M*N)

fprintf('Value of P should be in between 1 and %d',(M*N));

else

% Now every thing is correct, we can work further

% Finding left neighbours if exist and storing in neigh

neighs=[];

i=1; % index counter

% left neighbour is (P-M)th cell if its lies between 1 to N*M

if (P-M) > 0

% finding upper left neighbour if P%M != 1 i.e. P is not upperrow cell

if mod(P, M) ~=1

neighs(i)=P-M-1; % storing in index

i=i+1; % incementing counter

end

neighs(i)=P-M; % storing in index

i=i+1; % incementing counter

% finding bottom left neighbour if P%M != 0 i.e. P is not lowest row cell

if mod(P, M) ~=0

neighs(i)=P-M+1; % storing in index

i=i+1; % incementing counter

end

end

% upper and lower neighbour of Pth cell

% finding upper neighbour if P%M != 1 i.e. P is not upperrow cell

if mod(P, M) ~=1

neighs(i)=P-1; % storing in index

i=i+1; % incementing counter

end

% finding bottom neighbour if P%M != 0 i.e. P is not lowest row cell

if mod(P, M) ~=0

neighs(i)=P+1; % storing in index

i=i+1; % incementing counter

end

% left neighbour is (P-M)th cell if its lies between 1 to N*M

if (P+M) <= (N*M)

% finding upper right neighbour if P%M != 1 i.e. P is not upperrow cell

if mod(P, M) ~=1

neighs(i)=P+M-1; % storing in index

i=i+1; % incementing counter

end

neighs(i)=P+M; % storing in index

i=i+1; % incementing counter

% finding upper left neighbour if P%M != 0 i.e. P is not lowest row cell

if mod(P, M) ~=0

neighs(i)=P+M+1; % storing in index

i=i+1; % incementing counter

end

end

% display

fprintf('Cell ID: %d\n',P);

fprintf('Neighbors: ');

fprintf('%d ',neighs);

end

CODE IMAGE

1 - clc; clear; 3 4 5 - % Prompting user to enter M, N and P and converting it to integer M=int32(input(Enter Number of rows

32 33 34 35 - 36 37 38 39 - 40 41 - 42 43 44 - 45 - 46 47 - 48 if mod(P, M) ~=0 neighs (i)=P-M+1; % storing in index i=i+1; %

1 63 64 65 66 % display fprintf(Cell ID: %d\n,P); fprintf(Neighbors: ); fprintf(%d, neighs); end - 67 -

OUTPUT IMAGE

1.

Enter Number of rows: 1 Enter Number of columns: 3 Enter Cell Number: 1 Please enter value of row and column >= 2

2.

Enter Number of rows: 4 Enter Number of columns: 6 Enter Cell Number: 27 Value of p should be in between 1 and 24

3.

Enter Number of rows: 4 Enter Number of columns: 6 Enter Cell Number: 4 Cell ID: 4 Neighbors: 378

4.Enter Number of rows: 4 Enter Number of columns: 6 Enter Cell Number: 18 Cell ID: 18 Neighbors: 13 14 15 17 19 21 22 23

Add a comment
Know the answer?
Add Answer to:
2. Neighbor Identification. MATLAB language Many engineering problems can be solved numerically by dividing a large,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. Let F(n, m) denote the number of paths from top-left cell to bottom-right cell in a (n x m) grid (that only permits moving right or moving down). It satisfies the recurrence relation F(n, m) F(n-1...

    5. Let F(n, m) denote the number of paths from top-left cell to bottom-right cell in a (n x m) grid (that only permits moving right or moving down). It satisfies the recurrence relation F(n, m) F(n-1, m) + F(n, m-1) What should be the initial condition for this recurrence relation? (Hint: What would be the number of paths if there was only a single row or a single column in the grid?)[5] Convince yourself that F(n, m) gives correct...

  • Question 2 Problem Definition Write a MATLAB script that uses nested loops to create a 100 by 100...

    Question 2 Problem Definition Write a MATLAB script that uses nested loops to create a 100 by 100 element 2D array representing an image that shades from white at the top to black at the bottom. Your script should save the image in a file called test2.png. Testing The image file produced should be shaded from white at the top of the image to black at the bottom of the image like SO: Coding Write your code in a MATLAB...

  • Question 2 In the minimum grid-path problem we are given a two-dimensional grid, where each point...

    Question 2 In the minimum grid-path problem we are given a two-dimensional grid, where each point on the grid has a value, which is a non-negative integer. We have to find a path from the top left corner of the grid to the bottom right corner, where each single step on the path must lead from one grid point to its neighbour to the right or below. The cost of the path is the sum of all values of the...

  • the picture above is the one you are supposed to use for the MATlab code please...

    the picture above is the one you are supposed to use for the MATlab code please help Problems. Grayscale images are composed of a 2D matrix of light intensities from O (Black) to 255 (White). In this lab you will be using grayscale images and do 2D-array operations along with loops and iflelse branches. 1. We can regard 2D grayscale images as 2D arrays. Now load the provided image of scientists at the Solvay Conference in 1927 using the imread)...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • Can you please help me with this question. Much appreciated. So I have a sample code,...

    Can you please help me with this question. Much appreciated. So I have a sample code, but we need to change the code in a way that fits the problem. A planar slab of material is initially at a temperature of 300 K. At the time t=0, microwave radiation is directed at the material, leading to internal heat generation that is uniform within the material. The sides of the slab experience negligible heat losses and a water bath maintains the...

  • 2. Let's now use Excel to simulate rolling two 6-sided dice and finding the minimum of...

    2. Let's now use Excel to simulate rolling two 6-sided dice and finding the minimum of both dice. • Create a new Excel sheet in your document. Click on cell Al, then click on the function icon f. and select Math&Trig, then select RANDBETWEEN. In the dialog box, enter 1 for bottom and enter 6 for top. • After getting the random number in the first cell, click and hold down the mouse button to drag the lower right corner...

  • PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the...

    PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the map referred to is pictured in the screenshot. Need help with these 4 tasks: Function 4: def is_map(map) : given a map (see screenshot), does it meet the following criteria? 1) it is a list of lists of values of type int; 2) it has at least one row and one column; 3) it’s rectangular (all sub-lists are same length); 4) only non-negative ints...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

    For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT