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

function Mat = makemat(M1,M2,M3) Mat = M1; %replace with your code end Write a M

ID: 3551807 • Letter: F

Question

function Mat = makemat(M1,M2,M3)
Mat = M1; %replace with your code
end


Write a MATLAB function M-file named make mat to receive three row arrays as input, and from them, create and return a matrix with three rows as output You may not assume that the row arrays are the same size. You will pad the shorter arrays with zeroes to make up the difference. The shortest array(s) should be right-justified (padded with zeroes on the left) The middle sized array should be left-justified (padded with zeroes on the right) The longest array should have no padding Your code should not allow any input that isn't a row array. The program should halt, inform the user of the mistake, and should return no output in this case. Hint: You could use built-in function size . From MATLAB help: D = size(X), for M-by-N matrix X, returns the two-element row vector D = [M,N] containing the number of rows and columns in the matrix

Explanation / Answer


Dropbox-link:

https://dl.dropboxusercontent.com/u/78383401/chegg/makemat.m


__________________________________________________________________________________________



makemat function:


function Mat = makemat(M1,M2,M3)
    size_m1=size(M1);
    size_m2=size(M2);
    size_m3=size(M3);
  
    %checking if all the inputs are row vectors or not
    if(size_m1(1) ~= 1)
        error('M1 is not a row vector');
    elseif(size_m2(1) ~= 1)
        error('M2 is not a row vector');
    elseif(size_m3(1) ~= 1)
        error('M3 is not a row vector');
    else
        sizes=[size_m1(2),size_m2(2),size_m3(2)];
        sort_sizes=sort(sizes);
        longest = sort_sizes(3); %find size of longest row vector
        Mat = zeros(3,longest); %creating 3xlongest matrix
      
        if(size_m1(2) == sort_sizes(3)) %M1 is longest
            Mat(1,:) = M1;
        elseif(size_m1(2) == sort_sizes(2)) %M1 is middle sized
            Mat(1,1:size_m1(2))=M1;
        elseif(size_m1(2) == sort_sizes(1)) %M1 is shortest
            start = longest-size_m1(2) +1;
            Mat(1,start:size_m1(2))=M1;
        end
      
        if(size_m2(2) == sort_sizes(3)) %M2 is longest
            Mat(2,:) = M2;
        elseif(size_m2(2) == sort_sizes(2)) %M2 is middle sized
            Mat(2,1:size_m2(2))=M2;
        elseif(size_m2(2) == sort_sizes(1)) %M2 is shortest
            start = longest-size_m2(2) +1;
            Mat(2,start:size_m2(2))=M2;
        end  
          
        if(size_m3(2) == sort_sizes(3)) %M3 is longest
            Mat(3,:) = M3;
        elseif(size_m3(2) == sort_sizes(2)) %M3 is middle sized
            Mat(3,1:size_m3(2))=M3;
        elseif(size_m3(2) == sort_sizes(1)) %M3 is shortest
            start = longest-size_m3(2) +1;
            Mat(3,start:longest)=M3;
        end
      
    end
              
end


_________________________________________________________________________________________



Output: