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

**Write a MATLAB function C=matshuffle(A,B) whose arguments are two matrices A a

ID: 639779 • Letter: #

Question

**Write a MATLAB
function C=matshuffle(A,B)
whose arguments are two matrices A and B.
If the two matrices are not of the same size, the function breaks with an error message; otherwise, an output matrix C is formed in the way that its odd numbered columns are the ordered columns of A and its even numbered columns are the ordered columns of B.
Important Note: the output matrix C must have twice as many columns as A (or B).
The function could use a single for loop to construct matrix C.
**Type the function matshuffle in your diary file and determine the output matrices C by running the function matshuffle(A,B) on each of the sets of matrices:
(a) A=magic(6)
B=eye(6)
(b) A=ones(6)
B=eye(5)

Explanation / Answer

function C=matshuffle(A,B)
[m,n]=size(A);
[m1,n1]=size(B);
if m==m1 && n==n1
C=zeros(m,2*n);
for i=1:2*n
if rem(i,2)==0
C(:,i)=B(:,(i/2));
else
C(:,i)=A(:,floor(i/2)+1);
end
end
else
disp('matrix dimension does not match')
end