【问题标题】:How to find the index of dependent column in sparse matrix如何在稀疏矩阵中找到依赖列的索引
【发布时间】:2015-01-14 01:06:17
【问题描述】:

我有一个稀疏矩阵,例如

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

现在,对于每次迭代,我将为每一列输入随机值

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

我的任务是找到依赖列的索引,并用依赖列中所有元素的零值重新标记它。例如,全矩阵 A 可以假设为

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

我们看到第二列是第一列的依赖行。所以我的预期结果如

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

你能帮我解决这个问题吗?

【问题讨论】:

    标签: matlab matrix


    【解决方案1】:

    我不知道您为什么要在稀疏上下文中讨论这个问题,但除了简单地检查每个依赖项之外,您还可以同时进行。我建议像

    %% 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
    

    示例输出

    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
    

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 2014-05-12
      • 2022-01-15
      • 2018-05-06
      • 2017-10-23
      • 2019-03-12
      • 2011-11-13
      • 1970-01-01
      • 2015-07-24
      相关资源
      最近更新 更多