Please help me with these 3 matlab questions! 1. Write a MATLAB function with th
ID: 3870130 • Letter: P
Question
Please help me with these 3 matlab questions!
1. Write a MATLAB function with the header
function [M1, M2] = mySplitMatrix(M)
where M is a matrix, M1 is the left half of M, and M2 is the right half of M. In the case where there is an odd number of columns, the middle column should go to M1. Hint: the size and mod functions should be helpful.
3.
(2) Write a MATLAB function with the header function [M] = myCheckerBoard(n) where M e RnXn has the following form: 1 01 0 1 and n is a positive integer. Note that the upper-left element should always be 1.Explanation / Answer
Question 1: Matlab code:
function [M1,M2] = mySplitMatrix(M)
mid = fix(size(M,2)/2) + rem(size(M,2), 2);
M1 = M(:,1:mid);
M2 = M(:,mid+1:size(M,2));
end
Sample test:
Output:
Sample test:
Output:
m1 =
Question 2: Matlab code:
function [M] = myCheckerboard(n)
start = 1;
M = [];
for j = 1:n
row = [];
tmp = start;
for i = 1:n
row = [row,tmp];
if(tmp == 1)
tmp = 0;
else
tmp = 1;
end
end
M = [M;row];
if(start == 1)
start = 0;
else
start = 1;
end
end
end
Sample Output:
>> M = myCheckerboard(6)
M =
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
>> M = myCheckerboard(5)
M =
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
Question 3:
function[C] = MyMatMult(A,B)
m = size(A,1); k = size(A,2);
n = size(B,2);
C = zeros(m,n);
ii = 1; jj = 1; kk = 1;
for ii = 1:m
for jj = 1:n
for kk = 1:k
C(ii,jj) = C(ii,jj) + (A(ii,kk) * B(kk,jj));
end
end
end
end
Sample Test:
A = rand(3,4);
B = rand(4,5);
C1 = MymatMult(A,B);
C2 = A*B;
norm(C2-C1)
(C2-C1)
Sample Output:
ans =
0
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.