John
John

Reputation: 3070

How to find the index of dependent column in sparse matrix

I have a sparse matrix such as

A=sparse(zeros(10,20))% 10 rows by 20 columns

Now, for each iteration, I will put random value for each column

for i=1:20
%% insert value - it is done
end

My task is that find the index of dependent column and replate it by zeros values for all element in the dependent column. For example, the full matrix A can assume as

full(A)=
        [ 
          1 1 1
          0 0 1
          1 1 1 
         ]

We see that the second column is a dependent row with first column. So my expected result such as

   full(A)=
        [ 
          1 0 1
          0 0 1
          1 0 1 
         ]

Could you help me resolve this problem?

Upvotes: 0

Views: 283

Answers (1)

matheburg
matheburg

Reputation: 2180

I don't know why you want discuss this in the sparse context, but apart from simply checking each dependency you could do it simultaneously. I suggest something like

%% create some data...
A = double(rand(3,3) > 0.5)

%% create linearDependencyMatrix with                  % important
%  linearDependencyMatrix(i,j) == 1                    % part
%            iff                                       %
%  column i and column j are linearly dependent        %
AA = A'*A;                                             %
normCols = diag(AA);                                   %
normMat = normCols*normCols';                          %
linearDependencyMatrix = abs(AA.^2-normMat) < 10*eps   %

%% set linearly dependent columns to 0
nonzeroCols = find(sum(abs(A)) ~= 0);
numCols = size(A,2);
for i = nonzeroCols
    for j = i+1:numCols
        if linearDependencyMatrix(i,j)
            A(:,j) = 0;
        end
    end
end
A

Example Output

A =
     1     0     0
     1     0     0
     1     1     1


linearDependencyMatrix =
     1     0     0
     0     1     1
     0     1     1


A =
     1     0     0
     1     0     0
     1     1     0

Upvotes: 1

Related Questions