IN MATLAB Given variable A that contains a matrix with at least two rows, swap t
ID: 3886527 • Letter: I
Question
IN MATLAB
Given variable A that contains a matrix with at least two rows, swap the first & last rows, as in the previous problem. Multiply the swapped matrix element-by-element to variable A, and assign this result to variable B
Examples:
Input A = [1 2 3
4 5 6]
Output B = [4 5 6 * [1 2 3 = [4 10 18
1 2 3] 4 5 6] 4 10 18]
function B = SwapRowsMult1(A)
%replace the line below with code to swap the 1st and last row of A, array multiply it to A & store the result into B
B = A;
end
Explanation / Answer
function B = SwapRowsMult1(A) s=size(A); B(1,1:s(2))=A(s(1),1:s(2)); B(s(1),1:s(2))=A(1,1:s(2)); if s(1) > 2 B(2:s(1)-1,1:s(2))=A(2:s(1)-1,1:s(2)); end for i=1:s(1) for j=1:s(2) B(i,j)=A(i,j)*B(i,j); end end end