Question

In Matlab create a rref() function from scratch given the matrix A

In Matlab create a rref() function from scratch given the matrix A

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

clc%clears screen
clear all%clears history
close all%closes all files
format long
clear all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAKES RANDOM MATRIX A
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m = 3;
A=randi([1,10],3,3);
BB=A;
[m,n] = size(A);
type='f';
tol=1e-5;
do_full = ~issparse(A) || strcmp(type,'f');
if do_full
% Loop over the entire matrix.
i = 1;
j = 1;
jb = [];
% t1 = clock;
while (i <= m) && (j <= n)
% Find value and index of largest element in the remainder of column j.
[p,k] = max(abs(A(i:m,j))); k = k+i-1;
if (p <= tol)
% The column is negligible, zero it out.
A(i:m,j) = 0; %(faster for sparse) %zeros(m-i+1,1);
j = j + 1;
else
% Remember column index
jb = [jb j];
% Swap i-th and k-th rows.
A([i k],j:n) = A([k i],j:n);
% Divide the pivot row by the pivot element.
Ai = A(i,j:n)/A(i,j);
% Subtract multiples of the pivot row from all the other rows.
A(:,j:n) = A(:,j:n) - A(:,j)*Ai;
A(i,j:n) = Ai;
i = i + 1;
j = j + 1;
end
end
else
% Non-pivoted Q-less QR decomposition computed by Matlab actually
% produces the right structure (similar to rref) to identify independent
% columns.
R = qr(A);
% i_dep = pivot columns = dependent variables
% = left-most non-zero column (if any) in each row
% indep_rows (binary vector) = non-zero rows of R
[indep_rows, i_dep] = max(R ~= 0, [], 2);
indep_rows = full(indep_rows); % probably more efficient
i_dep = i_dep(indep_rows);
i_indep = setdiff(1:n, i_dep);
% solve R(indep_rows, i_dep) x = R(indep_rows, i_indep)
% to eliminate all the i_dep columns
% (i.e. we want F(indep_rows, i_dep) = Identity)
F = sparse([],[],[], m, n);
F(indep_rows, i_indep) = R(indep_rows, i_dep) \ R(indep_rows, i_indep);
F(indep_rows, i_dep) = speye(length(i_dep));
% result
A = F;
jb = i_dep;
end
disp('RREF is');
disp(A);

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
In Matlab create a rref() function from scratch given the matrix A
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT