Question

In: Electrical Engineering

MATLAB ONLY: Write a user defined method MaxPath() Find the maximum path sum in matrix. The...

MATLAB ONLY:

Write a user defined method MaxPath()

Find the maximum path sum in matrix. The maximum path is sum of all section from first to last
row where you can move only down or at a angle to left or right. You can start from any section
in first row of given matrix of P*Q.
Input: mat [] [] = 10 10 2 0 20 4
1 0 0 30 2 5
0 10 4 0 2 0
1 0 2 20 0 4
Output: 74
The maximum sum path is 20-30-4-20.
Input: mat [] [] = 1

Solutions

Expert Solution

When you directly copy paste the code in the MATLAB Editor you will get error because the tab spaces will be converted into blank spaces. To resolve the error replace the blank spaces with appropriate tab spaces. You can use the screenshot of the code for number of tab spaces required.

X=[10 10 2 0 20 4;
1 0 0 30 2 5;
0 10 4 0 2 0;
1 0 2 20 0 4];
MaxPath(X)
function sum = MaxPath(mat)
[p,q] = size(mat);
res = -1;
for i = 1:q
res = max([res mat(1,i)]);
end
for i = 2:p
res = -1;
for j = 1:q
if ((j>1) && (j<q))
mat(i,j) = mat(i,j) + max([mat(i-1,j) max([mat(i-1,j-1) mat(i-1,j+1)])]);
elseif (j>1)
mat(i,j) = mat(i,j) + max([mat(i-1,j) mat(i-1,j-1)]);
elseif (j<q)
mat(i,j) = mat(i,j) + max([mat(i-1,j) mat(i-1,j+1)]);
end
res = max(mat(i,j),res);
end
end
sum = res;
end

X=[10 10 2 0 20 4; % Test Matrix for verification
1 0 0 30 2 5;
0 10 4 0 2 0;
1 0 2 20 0 4];
MaxPath(X) % Calling the function MaxPath for verification
function sum = MaxPath(mat) % Start of function MaxPath()
[p,q] = size(mat); % To find matrix size
res = -1; % To find max value in first row
for i = 1:q
res = max([res mat(1,i)]);
end
for i = 2:p
res = -1;
for j = 1:q % When all paths are possible
if ((j>1) && (j<q))
mat(i,j) = mat(i,j) + max([mat(i-1,j) max([mat(i-1,j-1) mat(i-1,j+1)])]);
elseif (j>1) % When diagonal right is not possible
mat(i,j) = mat(i,j) + max([mat(i-1,j) mat(i-1,j-1)]);
elseif (j<q) % When diagonal left is not possible
mat(i,j) = mat(i,j) + max([mat(i-1,j) mat(i-1,j+1)]);
end
res = max(mat(i,j),res); % Store max path sum
end
end
sum = res;
end % End of function MaxPath()

The below output is obtained for the given test data.


Related Solutions

Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB...
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB function should be voltage across the load (in Vrms, assume 0 phase), frequency Resistance of the load Inductance of the load power factor of the load target power factor The output of the function should be the size of the capacitor that one would need to place in parallel with the load to reach the target power factor. Please submit the code for the...
Write a user-defined MATLAB function that uses classical fourth order Runge-Kutta method to solve a first...
Write a user-defined MATLAB function that uses classical fourth order Runge-Kutta method to solve a first order ODE problem dydx = f(x, y) in a given interval a ? x ? b with initial condition y(a) = y0 and step size of h. For function name and arguments, use [x,y] = myrk4(f, a, b, h, y0) Check your function to find the numerical solution for dydx=?1.2y+7e^(?0.3x) in the interval 0 ? x ? 4 with initial condition y(0)=3. Run your...
MATLAB Write a user defined function for a projectile motion. If a ball is launched from...
MATLAB Write a user defined function for a projectile motion. If a ball is launched from initial position(0,0) with a velocity v0 at angle θ, determine your horizontal and vertical position. Please plot x vs. t, y vs. t and y vs. x.
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate the sum of each column. Use subscript notation to find the 6th element and the last element of each magic square. Find the maximum of all the elements in each magic square.
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
Write a user-defined MATLAB function, with two input and two output arguments that determines the height...
Write a user-defined MATLAB function, with two input and two output arguments that determines the height in centimeters (cm) and mass in kilograms (kg)of a person from his height in inches (in.) and weight in pounds (lb). (a) Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. (b) Determine your own height and weight in SI units.
Write a Matlab function for a matrix that takes in a matrix in echelon form and...
Write a Matlab function for a matrix that takes in a matrix in echelon form and will return the row canonical form. The function cannot use rref, or any other matlab built in functions.
How to write a self-defined function to find the inverse of a matrix by using Gaussian...
How to write a self-defined function to find the inverse of a matrix by using Gaussian Jordan method in MATLAB? Thanks.
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT