a. Prompt the user for a number n. b. Use nested for loops to define a square ma
ID: 3807527 • Letter: A
Question
a. Prompt the user for a number n. b. Use nested for loops to define a square matrix m such that m(row, col) = row + col- 1. For example, if n = 3, m = 1 2 3 2 3 4 3 4 5 c. Call the function "reverse", as define below, passing the matrix defined in part b. and getting back that same matrix in reverse order d. Display both matrices in the command window one above the other Called "reverse" that takes a matrix as its one parameter and returns that same matrix but flipped about the diagonal axis from the lower left to the upper- For example, the 3 times 3 matrix above becomes: 5 4 3 4 3 2 3 2 1Explanation / Answer
driver.m
n = input('Enter the value of n');
M = zeros(n,n);
for i = 1:n
for j = 1:n
M(i,j) = i + j - 1;
end
end
original = M
reversed = reverse(M)
reverse.m
function [out] = reverse(M)
tmp = zeros(size(M,1),size(M,2));
out = zeros(size(M,1),size(M,2));
for i = 1:size(M,1)
tmp(:,i) = M(size(M,1) +1 - i,:)';
end
for i = 1:size(M,1)
out(i,:) = tmp(size(M,1) + 1 - i,:);
end
end
Sample Output:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.