matlab uses logical matrix as index

Learning matlab today and seeing a usage, I am very confused. The code is as follows:

Dx(isfinite(Gx),:)

isfinite(Gx) is a two-dimensional logical matrix. What does it mean to use a logical matrix as an index?

So try:

First create a logical matrix at random

logic = isfinite([1,nan;1,1;1,1]);

logic =

  3×2 logical 数组

   1   0
   1   1
   1   1

Create a 2D random matrix

>> m = rand(4,4)

m =

    0.2967    0.0855    0.9289    0.2373
    0.3188    0.2625    0.7303    0.4588
    0.4242    0.8010    0.4886    0.9631
    0.5079    0.0292    0.5785    0.5468

try the result

>> m(logic,:)
位置 1 处的逻辑索引包含一个在数组边界之外的 true 值。

It's beyond the limit, but it stands to reason that the dimension of the logical matrix is ​​smaller than the dimension of the random matrix.

Then expand the dimension of the random matrix and try

>> m = rand(6,6)

m =

    0.5211    0.3674    0.0987    0.1068    0.8909    0.5000
    0.2316    0.9880    0.2619    0.6538    0.3342    0.4799
    0.4889    0.0377    0.3354    0.4942    0.6987    0.9047
    0.6241    0.8852    0.6797    0.7791    0.1978    0.6099
    0.6791    0.9133    0.1366    0.7150    0.0305    0.6177
    0.3955    0.7962    0.7212    0.9037    0.7441    0.8594

>> m(logic,:)

ans =

    0.5211    0.3674    0.0987    0.1068    0.8909    0.5000
    0.2316    0.9880    0.2619    0.6538    0.3342    0.4799
    0.4889    0.0377    0.3354    0.4942    0.6987    0.9047
    0.6791    0.9133    0.1366    0.7150    0.0305    0.6177
    0.3955    0.7962    0.7212    0.9037    0.7441    0.8594

As you can see, one line is missing, and the fourth line is gone.

Then come to think, why the logical matrix

logic =

  3×2 logical 数组

   1   0
   1   1
   1   1

Can make the fourth row disappear?

>> logic(:)

ans =

  6×1 logical 数组

   1
   1
   1
   0
   1
   1

>> logic(4)

ans =

  logical

   0

It can be seen that the linear index (column index) is used to traverse the logical matrix, and its fourth element is exactly logic 0

It seems that the logical matrix is ​​used as the (row) index, but in fact, the logical matrix is ​​linearly indexed, and only the rows with logic 1 are reserved.

The same is not preserved if the number of matrix rows exceeds the linear index of the logical matrix

>> m = rand(20,6);
>> m(logic,:)

ans =

    0.8055    0.7224    0.6569    0.4177    0.1564    0.5841
    0.5767    0.1499    0.6280    0.9831    0.8555    0.1078
    0.1829    0.6596    0.2920    0.3015    0.6448    0.9063
    0.8865    0.9730    0.0155    0.6663    0.1909    0.8178
    0.0287    0.6490    0.9841    0.5391    0.4283    0.2607

Still only 5 lines.

The same is true for using logical indexes as column indexes

>> m = rand(6,6)

m =

    0.0688    0.7184    0.7788    0.4401    0.6377    0.6951
    0.3196    0.9686    0.4235    0.5271    0.9577    0.0680
    0.5309    0.5313    0.0908    0.4574    0.2407    0.2548
    0.6544    0.3251    0.2665    0.8754    0.6761    0.2240
    0.4076    0.1056    0.1537    0.5181    0.2891    0.6678
    0.8200    0.6110    0.2810    0.9436    0.6718    0.8444

>> m(:, isfinite([1,1;nan,1]))

ans =

    0.0688    0.7788    0.4401
    0.3196    0.4235    0.5271
    0.5309    0.0908    0.4574
    0.6544    0.2665    0.8754
    0.4076    0.1537    0.5181
    0.8200    0.2810    0.9436

Only columns 1, 2, and 4 with a logical index of 1 are kept.

So what if the logical index is traversed in the form of a linear index?

>> logic = isfinite([1,nan;1,1]);
m = rand(4,4)
m(logic)

m =

    0.3804    0.5308    0.5688    0.1622
    0.5678    0.7792    0.4694    0.7943
    0.0759    0.9340    0.0119    0.3112
    0.0540    0.1299    0.3371    0.5285


ans =

    0.3804
    0.5678
    0.0540

It can be seen that only the elements of the original matrix corresponding to the linear index of the logic 1 position of the logic matrix are reserved. and output the result as a column vector.

So it's still the same sentence:

The logical matrix is ​​used as a row (column) index, in fact, the logical matrix is ​​linearly indexed, and only the row (column) of the original matrix whose logic is 1 is reserved.

Another interesting usage

Using the logic matrix as an index, all the elements of the original matrix can be taken out by column, only the elements of the compound logic in the matrix are kept, and the final result is output as a column vector

If you want to take out all the finite elements in the matrix m, you can use

m(isfinite(m))

As a result, m will be taken out by column, the elements that are nan will be removed, and finally arranged into a column vector.

Guess you like

Origin blog.csdn.net/Eason_Y/article/details/129289208