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

Given any square matrix M with even dimensions (2 by 2, 4 by 4, 6 by 6, and so o

ID: 3803787 • Letter: G

Question

Given any square matrix M with even dimensions (2 by 2, 4 by 4, 6 by 6, and so on). a) Write a MATLAB function: fold HM= fold H(M), that will return in matrix foldHM^- a matrix that is the horizontally folded version of M. both horizontal halves of M should be summed as shown below. Given the 4 by matrix M 1 -3 2 4 5 7 8 9 6 8 21 3 3 3 3 1 -3 2 4 5 7 8 9 6 8 2 1 3 3 3 3 Calling the function foldH(M) gives the horizontally folded, 2 by 4, matrix foldHM. 4 0 5 7 11 15 10 10 Vectorize your code as much as you can. Your code should work with nay matrix of even dimensions. b) Write a MATLAB function: foldV(M) that will return in matrix "foldVM" a matrix that is the vertically folded version of M. Both vertical halves of M should be summed as shown below Given the same 4 by matrix M 1 - 3 2 4 5 7 89 6 8 2 1 3 3 3 3 Calling the function fold V(M) gives the vertically folded, 4 by 2, matrix, fold VM. 3 1 13 16 8 9 6 6 Vectorize your code as much as you can. You code should work with any matrix of even dimensions.

Explanation / Answer

Hello champ :)

So I have included the function for foldH(M) below, I have commented it for you. Please feel free to ask me if you face any trouble

function foldHM = foldH(M)
sz = size(M);
if (mod(sz(1),2) == 1) %we check if there are even rows, if not we cannot fold
    disp("There are odd rows and cannot be folded");
    return;
end
foldHM = rand(sz(1)/2,sz(2)); %we create a new array containing half rows as f M

for i = 1:sz(1)/2 %loop i itereates over the upper half of the matrix M
    for j = 1:sz(2)
        foldHM(i,j) = M(i,j) + M(sz(1)-i+1 ,j); %this is little tricky
                                                %we sum ith row from top with ith row from bottom
                                                %incase you have any confusion feel free to ask me
    end
end
end

and the function foldV(M) is given below :P

function foldHM = foldH(M)
sz = size(M);
if (mod(sz(2),2) == 1) %we check if there are even columns, if not we cannot fold
    disp("There are odd columns and cannot be folded");
    return;
end
foldVM = rand(sz(1),sz(2)/2); %we create a new array containing half columns as f M

for i = 1:sz(1) %loop i itereates over all rows of the matrix M
    for j = 1:sz(2)/2 %loop j will iterate over half the number of cols
        foldVM(i,j) = M(i,j) + M(i ,sz(2)/2 + j); %this is little different and less tricky
                                                %we sum jth col from left with jth row from half of center
                                                %incase you have any confusion please ask me
    end
end
end

I hope this answer helps you. You can ask me if you have any dificulty understanding. :)

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