Question
matlab( answer the question in matlab)
show the full answer with comment please.
W a rnction to find the largest product of adjacent number pairs in a matrix, where pair combinations can be selected the hor
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The MATLAB Code is given as follows:

function maxprod=maxpairs(a)
    [m,n]=size(a); % find the order of the matrix m-rows n-columns
    maxprod=0; % initialize maxproduct to 0
    for i=1:m
        for j=1:n
            %Find the neighbours of the given element a(i,j)
            neighbour=[0 0 0 0]; % an element in the matrix can have a maximum of 4 neighbours eliminating
            %the diagonal neighbours. So we initialize an array of size 4
            %to store the neighbours of each element in the matrix.
            % In case an element does not have a neighbour, the neigbour
            % value will be maintained as 0.
            if i-1>0 % TOP neighbour
                %top neighbour will not be available for the first row
                % elements. Hence i-1>0 condition should be verified
                % because i-1 will be 0 for top row elements
                neighbour(1)=a(i-1,j);
            end
            if i+1<=m %BOTTOM neighbour
                %bottom neighbour will not be available for the last row
                %elements. Hence i+1<=m condition should be verified
                %because i+1 will be >m for bottom row elements
                neighbour(2)=a(i+1,j);
            end
            if j+1<=n %RIGHT neighbour
                %right neighbour will not be available for the last column
                %elements. Hence j+1<=n condition should be because j+1
                %will be >n for last column elements
                neighbour(3)=a(i,j+1);
            end
            if j-1>0 %LEFT neighbour
                %left neighbour will not be available for the last column
                %elements. Hence j-1>0 condition should be verified because
                %j-1 will be 0 for first column elements
                neighbour(4)=a(i,j-1);
            end
            for k=1:4 % check the product of matrix element with every neighbour
                prod=a(i,j)*neighbour(k);
                if(prod>maxprod) %if the product is larger than maxprod, update maxprod with the value of prod
                    maxprod=prod;
                end
            end
        end
    end
end

TILL I function maxprod=maxpairs(a) [m, n]=size(a); $ find the order of the matrix m-rows n-columns maxprod=0; $initialize ma

felements. Hence 1+1<=n condition should be because 3+1 twill be >n for last column elements neighbour (3) =a (i, j+1); end i

OUTPUT:

>> a=randi (100, [37]) a = 82 80 65 38 82 54 52 88 >> c=maxpairs(a) 8272 fx >> |

>> b=randi (100, [5 6]) b = 23 93 44 27 30 27 18 44 12 613281 23 1926 72 433 44 91 41 23 51 93 32 98 60 12 9 74 >> c=maxpairs

Add a comment
Know the answer?
Add Answer to:
matlab( answer the question in matlab) show the full answer with comment please. W a rnction...
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
  • Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr,...

    Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example: >> A = randi(100,3,4) A = 66 94 75 18 4 68 40 71 85 76...

  • MATLAB simulate the game "Threes!" in MATLAB. The "game board" should take place on a 4x4...

    MATLAB simulate the game "Threes!" in MATLAB. The "game board" should take place on a 4x4 matrix. Simulate empty matrix cells with zeros. o a zero isan empaty space and a non-empty sG-) An example 0 2 2 1 Gameboard vw1 230 Thejndividual custom function should be able to take any ONE number within the gameboard id axci tme i funcon should uillixe branches io accout for all pssibl scenarios (the number is already against the wall, it is blocked...

  • MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and on...

    MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p, in which case C is an m × q matrix. 5. Recall that if A is an mx n matrix and B is a px q matrix, then the product C-AB is defined if and only if n = p, in which case C is...

  • answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace...

    answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace all elements divisible by 3 with 100. b) Replace all elements divisible by 2 with 50. c) Replace the first element of each row with 10. d) Display the all elements of the 2D matrix as a 10 array (9 elements in 1 row) 8) Write a function file that determines the maximum element of an input matrix A using nested loops and counts...

  • Please code in MatLab or Octave Output should match Sample Output in the second picture Thank...

    Please code in MatLab or Octave Output should match Sample Output in the second picture Thank You 4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x p matrix B, the matrix product C = AB is the m xp matrix with Cij = R=1 Qikbky. Write a program which calls a function get matrix.dimensions which should • print a description of the purpose of...

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the ...

    In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the way that is chosen by MATLAB, which is different from the textbook presentation. An mxn matrix A can be presented as a product of a unitary (or orthogonal) mxm matrix Q and an upper-triangular m × n matrix R, that is, A = Q * R . Theory: a square mxm matrix Q is called unitary (or orthogona) if -,or equivalently,...

  • This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an...

    This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an integer number n, and returns a matrix that is nxn, where the value of each number is the distance from the upper-left to bottom-right diagonal. (Use while loops if you can!) Numbers below the diagonal should be negative, and numbers above the diagonal should be positive. For example, if your input was 5, your output would be: 0 1 2 3 4 -1 0...

  • Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include...

    Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include two things: the PDF and the zipped folder. Late assignments are not accepted. For the questions below, do not create the arrays or matrices by hand. Instead, use built-in MATLAB functions and shorthand notations. Otherwise, no points will be given. 1) Create a function called my product that computes the multiplication of the numbers in any vector using a while loop. The function should...

  • Can you help me with this question please? For the code, please do it on MATLAB. Thanks

    Can you help me with this question please? For the code, please do it on MATLAB. Thanks 7. Bonus [3+3+4pts] Before answering this question, read the Google page rank article on Pi- azza in the 'General Resources' section. The Google page rank algorithm has a lot to do with the eigenvector corresponding to the largest eigenvalue of a so-called stochastic matrix, which describes the links between websites.2 Stochastic matrices have non-negative entries and each column sums to1, and one can...

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