Problem 2) Given any square matrix M with even dimensions ( 2 by 2,4 by 4, 6 by
ID: 2292168 • Letter: P
Question
Problem 2) Given any square matrix M with even dimensions ( 2 by 2,4 by 4, 6 by 6, and so on), ldHM-foldH(M), that will return in matrix ToklHM" a matrix Telded version of M. Both horizontal halves of M should be summed as a) write a MATLAB function: foldHM- foldH(M), that Example: Given the 4 by 4 matrix M that is the horizontally shown below of 1 3 24 1 3 2 4 97 Calling the function foldH(MO gives the horizontally folded, 2 by 4, matrix foldHM 11 15 10 10 Vectorize your code as moch as you can. Your code should work with any matrix of even dimensions. that is the vertically folded version of M. Both vertical halves of M should be summed as shown below (note that now, the sum of both halves is in different order than in part a) b) wrile a MATLAB function: foldVM foldV(M), that will return in matrix "foldVM" a matrix Example: Given the same 4 by 4 matrix M 1 3 2 4 1 3 I2 4 5 718 9 6 812 1 3 313 3 Calling the function foldV(M) gives the vertically folded, 4 by 2, matrix foldVM 13 16 Vectorize your code as much as you can. Your code should work with any matrix of even dimensions Insert discussion or calculations here:Explanation / Answer
code for horizontal folding is below
filename : foldH.m
--------------------------------------
function out = foldH( x )
%matrix must be of even dimensions as specified in question
i=1;
j=size(x,1);
out=[]; %output matrix
%horizontal folding matrix
while i<j
out=[out;x(i,:)+x(j,:)];
i=i+1;
j=j-1;
end
end
--------------------------------
code for vertical folding is below
filename: foldV.m
------------------------------------
function out = foldV( x )
%matrix must be of even dimensions as specified in question
i=1;
j=1+size(x,2)/2;
out=[]; %output matrix
%vertical folding code
while j<=length(x)
out=[out,x(:,i)+x(:,j)];
i=i+1;
j=j+1;
end
end
----------------------------------------
matlab commands and output
>> a=[1 -3 2 4; 5 7 8 9; 6 8 2 1; 3 3 3 3]
a =
1 -3 2 4
5 7 8 9
6 8 2 1
3 3 3 3
>> foldH(a)
ans =
4 0 5 7
11 15 10 10
>> foldV(a)
ans =
3 1
13 16
8 9
6 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.