Is this code correct? completed HW (basis of Null(A) Col(A) and Row(A))

Hey guys, im relatively new to matlab, and i just wanted to make sure my code is written correctly (it does work but habits is more important)
first program finds bases for RowA and ColA the second is a basis for NullA
function [C,R] = ColRowSpace (A)
%sets size for A
[m,n] = size(A);
% makes the rref of A, and finds the pivot columns in A
[B,jb] = rref(A);
%sets the length of jb
o = size(jb);
%this logical finds if a row is empty
%it will return [1 1 0 0] in which case the first two rows
%have values and the last two dont
l = logical(sum(B'));
%for loop from 1 to o+1
for i = 1:o+1
%the ith row of C is the jb(i)'th column of A
C(:,i)= A(:,(jb(i)));
% for exsample if jb = [ 1 3 4] then the first iteration is
% C(:,1) = A(:,1)
% the second is C(:,2) = A(:,3)
% the third is C(:,3) = A (:,4)
end
%Rowspace is set to the rref of rows that are not 0
R = B(l, :)';
end
Second function
function N = NullSpace(A)
%{NullSpace()takes an input matrix and reduces it to rref. From there it
%uses its reduced components to make a basis for Null(A). It is then
%returned to the user as N
[m,n] = size(A);
%sets the size of A
E = rref(A);
%reduces A to rref
A = logical(zeros(1,n));
% [ 0 0 0 0 ...0] length n
B = logical(ones(1,n));
% [ 1 1 1 1...1] length n
rows=1; columns=1; iterations=0;
%initializes values
while (columns < n+1 & rows < m+1 ),
%if columns or rows surpass n and m repectively, the loop stops
if E(rows,columns)==1,
%if E(rows,columns) is a pivot point
A(columns)=1;
% changes the columns'th 0 in A to a 1
B(columns)=0;
% changes the columns'th 1 in B to a 0
rows = rows+1;
%increments rows by one
iterations = iterations+1;
%increases iterations by one
end
columns = columns+1;
%increases columns by one
end
N = zeros(n,n-iterations);
% sets up the skeleton for N
N(B,:) = eye(n-iterations);
%inserts identity matrix into the skeleton
N(A,:) = -E(1:iterations,B);
%inserts negative values of the rref matrix into the skeleton
end
I basically want feed back to the structure of my program, maybe criticism on repetitive things, or warn me of bad habits
Thank you all.

Answers (1)

Hi Manuel,
I can't check the methodology, but here are a few comments based on what I could see in a glimpse.
About ColRowSpace()
l = logical(sum(B'));
can be rewritten using either ALL or ANY (look up the doc for them).
for i = 1:o+1
C(:,i)= A(:,(jb(i)));
end
The code above should not be working, as you are indexing jb with i greater than its length. The loop index could be renamed ii or k, but i and j should be reserved for complex numbers (even though you'll often see people using them as loop indices, even in books). The array C seems to be increasing in size iteratively (MLINT is probably indicating this to you in the editor), so you should consider preallocating memory for this variable, e.g. C = zeros(size(A)) before the loop, or whatever size is appropriate in your context. Finally, if you were trying to permute columns of A, you might be able to eliminate the preallocation and the loop and do something like
C = A(:,jb) ;
I don't think that it is exactly what you want, because of the dimension of A, C, jb, but you could exploit this syntax.. here is an illustration:
>> A = magic(3) % Some A matrix.
A =
8 1 6
3 5 7
4 9 2
>> C = A(:,[3,2,1]) % Permute columns 1,2,3 -> 3,2,1.
C =
6 1 8
7 5 3
2 9 4
About NullSpace()
A = logical(zeros(1,n));
can be rewritten using FALSE (e.g. false(3,2) produces a 3x2 array of logical falses). Same for B with TRUE. It seems to me actually that B will be the logical negation of A ultimately. If so, you could compute just A and after the while loop define B = ~A.
That's it for this first glimpse. Other than that, you could improve the indentation so you/we see the structure immediately without having to count the end keywords. I didn't check that your computations/approach was correct, but I liked the amount of comments, that will help people who will have to read/understand the code later (good practice!).

Asked:

on 18 Mar 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!