# How to get rid of linearly dependent columns of a given matrix on matlab

I'm having a hard time in trying to get rid of linearly dependent columns of a matrix on matlab. I was trying to run this code

for i=2:22
if rank(D(:,1:i))==i-1
D(:,i)=[];
end
end


for a 30x30 matrix with rank 22, which means that I have to get rid of 8 linearly dependent columns. In this code I am just getting rid of the first ld column and the the following one that is not ld. Does anyone know how to do that? Thanks!

## 1 Answer

If you do not mind using matlab built-in functions:

[~,colind] = rref(A);
B = A(:, colind);

• Thank you so much.It worked!!! – Lucas Mar 7 '15 at 4:22