Write a MATLAB function that converts a matrix to a lower tr
Write a MATLAB function that converts a matrix to a lower triangular structure. The function header is: function L=Lower (A) where the input A is a matrix and the output L is the factorized matrix with lower-triangular structure. Similar to the Upper function, you need to first check if A is a square matrix or not; then apply the elimination procedure to the proper rows.
Solution
function L = Lower(A)
n = length(A); % length of vector
L = zeros(n); % allocate memory
for j = 1:n % only loop over all columns
for i = 1:j % only the top half
L(i,j) = A(j); % copy the value from the vector to the matrix
end
end
You could also rewrite the loop using array notation:
for j = 1:n % only loop over all columns
L(1:j,j) = A(j); % copy the value from the vector to the matrix
end
Finally, you could use the repmat and triu commands to create the matrix instead of any loops.
L = repmat(A,n,1); % create a full matrix with each row as the vector
L = tril(L);
