【问题标题】:Extract positive and negative vales from Matrix从矩阵中提取正负值
【发布时间】:2013-09-12 09:50:12
【问题描述】:

我有这样的 3D 矩阵

1   5648.00278672228    -46.43159546036
1   5650.38906894239    68.81787768047
1   5649.13081105839    -4867.55961979647
1   5650.53055771227    4868.95936645035
1   5647.95215053492    4866.38095927300
1   5650.21656586142    -2328.64537459950
1   5651.76371933598    7870.19252807406
1   5649.87288540620    -1168.30169414428

我想将第三列的正负值提取到两个单独的二维矩阵(POS 和 NEG)中,其中第一列包含上述矩阵中行的索引,第二列包含该值。

结果应该是这样的

POS = 
2   68.81787768047
4   4868.95936645035
5   4866.38095927300
7   7870.19252807406

NEG =  
1   -46.43159546036
3   -4867.55961979647
6   -2328.64537459950
8   -1168.30169414428

【问题讨论】:

    标签: matlab matrix


    【解决方案1】:

    您可以使用find 函数和逻辑索引

    POS = [find(A(:,3)>0) A(A(:,3)>0,3)]
    NEG = [find(A(:,3)<=0) A(A(:,3)<=0,3)]
    

    因为你的输入矩阵是 A

    A = [1   5648.00278672228    -46.43159546036
        1   5650.38906894239    68.81787768047
        1   5649.13081105839    -4867.55961979647
        1   5650.53055771227    4868.95936645035
        1   5647.95215053492    4866.38095927300
        1   5650.21656586142    -2328.64537459950
        1   5651.76371933598    7870.19252807406
        1   5649.87288540620    -1168.30169414428]
    

    结果将是(format shortG

    POS =
    
            2       68.818
            4         4869
            5       4866.4
            7       7870.2
    
    
    NEG =
    
            1      -46.432
            3      -4867.6
            6      -2328.6
            8      -1168.3
    

    感谢@Dennis Jaheruddin 和 Khurram Majeed 的 cmets,第二个发现被删除了。

    【讨论】:

    • 每行的第二个 find 可以省略。
    • 感谢您的回答,仅供参考,我使用的是 Matlab 2013a,它提示我 To speed up and simplify the program, replace the call to find with logical array indexing.,当我点击自动修复时,它会删除第二列的 find
    【解决方案2】:

    这是另一种方法

    p = find(A(:,3)>0);
    n = find(A(:,3)<0);
    
    POS = [p A(p,3)]
    NEG = [n A(n,3)]
    

    请注意,这不包括零条目。例如,如果您希望它们在第一个向量中,请使用 &gt;= 而不是 &gt;

    【讨论】:

      【解决方案3】:

      这是另一种方式:

      B = [(1:size(A,1)).' A(:,3)];
      inds = B(:,2)>0;
      POS = B( inds,:);
      NEG = B(~inds,:);
      

      这比 Magla 的 find 解决方案更快:

      tic
      for ii = 1:1e5
          B = [(1:size(A,1)).' A(:,3)];
          inds = B(:,2)>0;
          POS = B( inds,:);
          NEG = B(~inds,:);
      end
      toc
      
      tic
      for ii = 1:1e5
          POS = [find(A(:,3)>0) A(A(:,3)>0,3)];
          NEG = [find(A(:,3)<=0) A(A(:,3)<=0,3)];
      end
      toc
      

      在我糟糕的机器上的结果:

      Elapsed time is 1.290744 seconds. % my new solution
      Elapsed time is 2.004015 seconds. % Magla's solution w/ find()
      

      这种差异部分是由于跳过find,否定逻辑索引而不是再次与&lt;= 0 比较,以及重新使用索引而不是重新计算它们。

      即使与更优化的 Magla 解决方案版本相比:

      tic
      for ii = 1:1e5
          inds = A(:,3)>0;
          POS = [find( inds) A( inds,3)];
          NEG = [find(~inds) A(~inds,3)];
      end
      toc
      

      我们发现

      Elapsed time is 1.290744 seconds. % my new solution
      Elapsed time is 1.476138 seconds. % Magla's solution w/ find()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-04
        • 1970-01-01
        相关资源
        最近更新 更多