Question

*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...

*** Write a function called reverse_diag that creates a square matrix whose elements are 0
except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an nby-
n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The
function takes one positive integer input argument named n, which is the size of the matrix, and
returns the matrix itself as an output argument. Note that using the built-in functions eye and
diag are not allowed. (Hint: you can index into a matrix with a single index and MATLAB will
handle it as if it was a vector using column-major order. Note that the grader will not test for n = 1,
but try to solve it for that case too.)

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

reverse_diag.m

function M = reverse_diag(n)
M = zeros(n);
M(sub2ind(size(M),size(M,1):-1:1,1:size(M,2))) = 1;
Add a comment
Know the answer?
Add Answer to:
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...
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
  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • An Triangular matrix is a square matrix whose elements below the diagonal are defined to be...

    An Triangular matrix is a square matrix whose elements below the diagonal are defined to be 0. For example, the matrix element Mr,c = 0 if r > c. The following is an example matrix of size 4. 0 1 2 3 0 100 200 300 400 1 0 500 600 700 2 0 0 800 900 3 0 0 0 1000 While it is possible to use a regular 2D array to represent an Triangular matrix, doing so is...

  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

  • on matlab (1) Matrices are entered row-wise. Row commas. Enter 1 2 3 (2) Element A,...

    on matlab (1) Matrices are entered row-wise. Row commas. Enter 1 2 3 (2) Element A, of matrix A is accesser (3) Correcting an entry is easy to (4) Any submatrix of Ais obtained by d row wise. Rows are separated by semicolons and columns are separated by spaces ner A l 23:45 6. B and hit the return/enter kry matrix A is accessed as A Enter and hit the returnerter key an entry is easy through indesine Enter 19...

  • Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting....

    Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting. That is, develop a function called mylu that is passed the square matrix [A] and returns the triangular matrices [L] and [U] and the permutation P. You are not to use MATLAB built-in function lu in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying...

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • Write a function to "smooth" a black-and-white image by replacing each pixel by the average of...

    Write a function to "smooth" a black-and-white image by replacing each pixel by the average of itself and its neighbors. In MATLAB a black-and-white image is just a matrix of 1s and 0s - 1 represents white, and 0 represents black. To keep the algorithm simple, ignore the edges of the image - that is, do not change the first and last rows and columns. function name = smooth_image() input argument = input matrix output argument = output matrix The...

  • 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...

  • Publish using a MatLab function for the following: If a matrix A has dimension n×n and has n line...

    Publish using a MatLab function for the following: If a matrix A has dimension n×n and has n linearly independent eigenvectors, it is diagonalizable.This means there exists a matrix P such that P^(−1)AP=D, where D is a diagonal matrix whose diagonal entries are made up of the eigenvalues of A. P is constructed by taking the eigenvectors of A and using them as the columns of P. Your task is to write a program (function) that does the following If...

  • 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,...

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