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

Write a MATLAB script called myodds that asks the user to input the matrix M. Us

ID: 2079700 • Letter: W

Question

Write a MATLAB script called myodds that asks the user to input the matrix M. Use M to Create a matrix called Modds that contains only those elements of M that are in odd rows and columns. In other words, it would return the elements of M at indices (1, 1), (1, 3), (1, 5), ..., (3, 1), (3, 3), (3, 5), ..., etc. Note that both the row and the column of an element must be odd to be included in the output. The following would not be returned: (1, 2), (2, 1), (2, 2) because either the row or the column or both are even. As an example, if M were a 5-by-8 matrix, then the output must be 3-by-4 because the MATLAB Script omits rows 2 and 4 of M and it also omits columns 2, 4, 6, and 8 of M. Test your Code with M = [1:10;11:20;21:30;31:40;41:50] and provide the output.

Explanation / Answer

Matlab Code::

function y = myodds(M)
y = [];
[r1 c1] = size(M);%retuns the given matrix's number of rows and columns
ry1 = 1;%results matrix row number
for i=1:r1
cy1 = 1;%resultant matrix column number
for j=1:c1
if rem(i,2)~=0 && rem(j,2)~=0%check for both rows and columns which should be odd
y(ry1,cy1)=M(i,j);
cy1 = cy1+1;
end
end
if rem(i,2)~=0%increments resultant matrix row number only when row number is odd
ry1 = ry1+1;
end
end
end

Command window output:

>> M

M =

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50

>> y = myodds(M)

y =

1 3 5 7 9
21 23 25 27 29
41 43 45 47 49

>>

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