Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given a number n, create an n-by-n matrix mat. Add all odd integers from 1 to n^

ID: 2080986 • Letter: G

Question

Given a number n, create an n-by-n matrix mat. Add all odd integers from 1 to n^2 next to each other (row by row) in ascending order starting with element 1,1. Add a n even integers from 2 to 2 next to each other (row by row) in descending order starting right after the l a^st odd integer, as shown in the examples below. Examples: Input n = 4 Output a = [1 3 5 7 9 8 6 4 2] Input n = 4 Output a = [1 3 5 7 9 11 13 15 16 14 12 10 8 6 4 2] Instructions: To solve this problem, modify the template bellow with your code. Leave the names of the function and variables unchanged. Also maintain the names of the sub-functions. Otherwise your code will not pass the test. Click Test to test your solution before submitting it. When you are satisfied with it click Submit.

Explanation / Answer

ANSWER:

%Take value of n from user
n = input ("Enter the value of n")

lim = n^2; %disp (lim);
r = rem (lim,2);

temp = zeros (lim,1);

for i=1:2:lim temp((i+1)/2) = i; end
idx = lim;
for i=2:2:lim temp(idx) = i; idx--; end

mat = zeros (n,n);

for i=1:1:n
mat(i,:) = temp ((1 + (n*(i-1))): n*i ); end
%disp (temp);
disp (mat);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote