线性索引,索引的逻辑,和所有 [英] Linear indexing, logical indexing, and all that

查看:154
本文介绍了线性索引,索引的逻辑,和所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们用于不同形式的索引在Matlab:


  • 标准(使用沿着每个维度的整数),

  • 逻辑(使用逻辑值),

  • 线性(使用单个索引遍历与多个尺寸的阵列)。

乍一看,它可能会出现这些形式独家:索引或者为标准,或逻辑,或线性。然而,有时似乎有几个的这些形式之间的共混物。例如,

 >> A =魔术(3)
A =
     8 1 6
     3 5 7
     4 9 2
>> A(A> 5)
ANS =
     8
     9
     6
     7

这是合乎逻辑的索引,对不对?但它也有线性索引的一些特点,因为返回的列向量。事实上,逻辑指数 A> 5 已为的线性指标发现同样的效果(A> 5)

作为第二个例子,考虑

 >> A =魔术(3)
A =
     8 1 6
     3 5 7
     4 9 2
>> A(1:2,[真假假真真])
ANS =
     8 6
     3 7

在这个前pression,标准(整数值)的索引被用于第一坐标和逻辑索引是用于第二

这些例子(并在实践中出现的更复杂的问题),提出以下几个问题:


  • 在Matlab是什么类型的索引呢?

  • 如何对它们进行组合?

  • 如何他们应该被称为?


解决方案

在以下我使用术语,我认为是或多或少符合标准的Matlab的做法。然而,在某些情况下,我不得不排序的做了一个名字,因为我不知道现有的。请让我知道,如果有更多的标准名字比我使用。

这答案试图阐明不同类型的索引以及如何进行组合。一个不同的问题是如何在形状尺寸)的输出数组被确定为指标变量的形状的功能。在一个很好的职位是索引的精华按罗兰舒尔。

下面的描述集中在数值数组的索引,但它可以被应用到电池阵列与任何括号或大括号的索引,具有明显的变化输出型(分别为单元阵列或逗号分隔的列表)。这将在年底简要讨论。

在数值阵列的索引的类型

索引可以分为考虑下面两个属性


  1. 据维数每个索引变量是指,索引可​​以是多维的或线性的。但这些只是两种极端情况。中间的情况存在,可以称为部分线性索引:


    • 多维索引指定数组的每个维度的指标变量。个别指数有时被称为下标 Matlab的文档中(例如,见 sub2ind )。

    • 纯的线性索引指定遍历所有尺寸阵列(这可以被看作是如果所有维度塌陷成一个)一个单一的指标变量。正如我们所知,遍历沿列第一,再沿行,然后沿着第三暗淡片等(所谓列-major订单)。

    • 部分线性索引:给出一个数组与 M + N 尺寸 N'GT; = 2 ,可以指定 M 第一个 M 尺寸(从而利用多维索引在这些方面)指数变量而在过去的 N 尺寸一个索引变量,这是间preTED为只有那些尺寸的线性指标(最后一个 N 尺寸塌陷成一个)。


  2. Acccording到索引值的类型,每个索引变量可以是整数值或逻辑:


    • 整数值如果索引包含可变正整数;

    • 逻辑如果索引变量包含逻辑值。


分类标准1和2的独立。但从标准1点指数的分类有按照标准2.其类别的所有组合都是可能没有任何关系。

因此​​,根据上述的分类,有索引6的基本类型即可。为了澄清,以下是对每一个例子。所有示例使用数组 A =猫(3,魔术(3),9 +魔力(3)),即

  A(:,:,1)=
     8 1 6
     3 5 7
     4 9 2
A(:,:,2)=
    17 10 15
    12 14 16
    13 18 11


  1. 多维,整数值:

     >> A([1〜2],2,2)
    ANS =
        10
        14


  2. 线性,整数值:

     >> A([2〜5:7])
    ANS =
         3 5 9 6


  3. 部分线性,整数值:

     >> A([1〜2],2:4)
    ANS =
         1 6 17
         5 7 12


  4. 多维的,合乎逻辑的:

     >> A([真真假],[假真假],[假真])
    ANS =
        10
        14

    有趣的是,逻辑值的数目可以更小,或甚至更大,比在尺寸大小的索引指的是:

     >> A([真真],[假真虚假],[假真])
    ANS =
        10
        14

    缺失值PTED为间$ P $假和剩余价值必须或错误会发生。例如见或的此页://计算器.COM / A /二百五十八万六千九百二十二分之二百四十六万二千八百九十八>此答案由乔纳斯


  5. 线性,逻辑:

     >> A([假假真真假假真真真])
    ANS =
         3 5 9 6

    (请注意,11尾的值已在索引矢量被忽略了。)


  6. 部分线性,逻辑:

     >> A([真真假],[假真真真虚假])
    ANS =
         1 6 17
         5 7 12


在多维或部分线性索引,其中存在多于一个的索引变量,每一个都可以独立地是整数值或逻辑。这样就产生了不同的混合型。例如:


  • 多维,逻辑/整型值:

     >> A([真假假真真],[假真真],2)
    ANS =
        10 15
        18 11


  • 部分线性,整数值/逻辑:

     >> A([1〜2],[真假真假真假])
    ANS =
         8 6 10
         3 7 14


  • 如果被索引数组是一个稀疏矩阵上述所有仍然适用,但部分线性索引对矩阵不存在的;当然,结果也稀疏

    电池阵列的索引

    数值阵列描述索引的所有类型的可应用于单元阵列,具有一个附加的考虑。电池阵列可以用括号或大括号进行索引。在第一种情况下,索引的结果是一个单元阵列。在第二个它是一个逗号分隔的单元格内容列表。

    作为一个例子,假设在previous例中使用的数值阵列被变换成单元阵列 C = num2cell(A)的,即,

      C(:,:,1)=
        [8] [1] [6]
        [3] [5] [7]
        [4] [9] [2]
    C(:,:,2)=
        [17] [10] [15]
        [12] [14] [16]
        [13] [18] [11]

    接着上面的例子8中使用会产生单元阵列的索引

     >> C([1〜2],[真假真假真假])
    ANS =
        [8] [6] [10]
        [3] [7] [14]

    而使用大括号将产生逗号分隔的列表

     >> C {1 [1〜2],[真假真假真假]}
    ANS =
         8
    ANS =
         3
    ANS =
         6
    ANS =
         7
    ANS =
        10
    ANS =
        14

    外卖消息/ TL; DR

    逻辑和线性索引并不排除类型的索引。相反,它们是索引的两个独立功能。 逻辑指的是索引值的类型,和线性表示多个维度被折叠和索引为一体。这两种功能可以同时发生。

    We are used to different forms of indexing in Matlab:

    • standard (using integers along each dimension),
    • logical (using logical values),
    • linear (using a single index to traverse an array with more than one dimension).

    At first sight, it may appear that these forms are exclusive: an index is either standard, or logical, or linear. However, sometimes there appears to be a blend between several of these forms. For example,

    >> A = magic(3)
    A =
         8     1     6
         3     5     7
         4     9     2
    >> A(A>5)
    ans =
         8
         9
         6
         7
    

    This is logical indexing, right? But it also has some features of linear indexing, because a column vector is returned. In fact, the logical index A>5 has the same effect as the linear index find(A>5).

    As a second example, consider

    >> A = magic(3)
    A =
         8     1     6
         3     5     7
         4     9     2
    >> A(1:2, [true false true])
    ans =
         8     6
         3     7
    

    In this expression, standard (integer-valued) indexing is used for the first coordinate, and logical indexing is used for the second.

    These examples (and more complicated ones that arise in practice) pose the following questions:

    • What types of indexing are there in Matlab?
    • How can they be combined?
    • How should they be referred to?

    解决方案

    In the following I use terminology that I think is more or less in line with standard Matlab practice. However, in some cases I've had to sort-of make up a name because I wasn't aware of an existing one. Please let me know if there are more standard names than those I'm using.

    This answer tries to clarify the different types of indexing and how they can be combined. A different question is how the shape (size) of the output array is determined as a function of the shape of the index variables. A good post on this is Essence of indexing by Loren Shure.

    The description to follow focuses on indexing of numerical arrays, but it can be applied to cell arrays with either parenthesis or curly-brace indexing, with the obvious change of output type (cell array or comma-separated list, respectively). This will be briefly discussed at the end.

    Types of indexing in numerical arrays

    Indexing can be classified considering the following two attributes.

    1. According to the number of dimensions each index variable refers to, indexing can be multidimensional or linear. But these are only two extreme cases. An intermediate situation exists, which may be termed partially linear indexing:

      • Pure multidimensional indexing specifies an index variable for each dimension of the array. The individual indices are sometimes referred to as subscripts in Matlab documentation (see for example sub2ind).
      • Pure linear indexing specifies a single index variable that traverses the array across all dimensions (this can be viewed as if all dimensions collapse into one). As we know, the traversal is along columns first, then along rows, then along third-dim slices, etc (so-called column-major order).
      • Partially linear indexing: given an array with m+n dimensions, n>=2, one can specify m index variables for the first m dimensions (thus using multidimensional indexing in those dimensions) and one index variable for the last n dimensions, which is interpreted as a linear index for those dimensions only (the last n dimensions collapse into one).
    2. Acccording to the type of the index values, each index variable can be integer-valued or logical:

      • It is integer-valued if the index variable contains positive integers;
      • It is logical if the index variable contains logical values.

    Classification criteria 1 and 2 are independent. The category of the index from the point of view of criterion 1 has no relationship with its category according to criterion 2. All combinations are possible.

    Thus, according to the above classification, there are 6 basic types of indexing. To clarify, following is an example for each. All examples use the array A = cat(3, magic(3), 9+magic(3)), that is,

    A(:,:,1) =
         8     1     6
         3     5     7
         4     9     2
    A(:,:,2) =
        17    10    15
        12    14    16
        13    18    11
    

    1. Multidimensional, integer-valued:

      >> A([1 2], 2, 2)
      ans =
          10
          14
      

    2. Linear, integer-valued:

      >> A([2 5:7])
      ans =
           3     5     9     6
      

    3. Partially linear, integer-valued:

      >> A([1 2], 2:4)
      ans =
           1     6    17
           5     7    12
      

    4. Multidimensional, logical:

      >> A([true true false], [false true false], [false true])
      ans =
          10
          14
      

      Interestingly, the number of logical values may be smaller, or even larger, than the size in the dimension the index refers to:

      >> A([true true], [false true false false], [false true])
      ans =
          10
          14
      

      Missing values are interpreted as false, and surplus values must be false or an error will occur. See for example this page by Mathworks or this answer by Jonas.

    5. Linear, logical:

      >> A([false true false false true true true])
      ans =
           3     5     9     6
      

      (Note that 11 trailing false values have been left out in the indexing vector.)

    6. Partially linear, logical:

      >> A([true true false], [false true true true false false])
      ans =
           1     6    17
           5     7    12
      

    In multidimensional or partially linear indexing, in which there is more than one index variable, each can independently be integer-valued or logical. This gives rise to different mixed types. For example:

    1. Multidimensional, logical/integer-valued:

      >> A([true false true], [false true true], 2)
      ans =
          10    15
          18    11
      

    2. Partially linear, integer-valued/logical:

      >> A([1 2], [true false true false true false])
      ans =
           8     6    10
           3     7    14
      

    If the array that is being indexed is a sparse matrix all of the above still applies, except that partially linear indexing doesn't exist for matrices; and of course the result is also sparse.

    Indexing of cell arrays

    All the types of indexing described for numerical arrays can be applied to cell arrays, with one additional consideration. Cell arrays can be indexed with parentheses or with curly braces. In the first case the result of the indexing is a cell array. In the second it is a comma-separated list of the cell contents.

    As an example, suppose the numerical array used in the previous examples is transformed into the cell array C = num2cell(A), that is,

    C(:,:,1) = 
        [8]    [1]    [6]
        [3]    [5]    [7]
        [4]    [9]    [2]
    C(:,:,2) = 
        [17]    [10]    [15]
        [12]    [14]    [16]
        [13]    [18]    [11]
    

    Then the indexing used in example 8 above would yield the cell array

    >> C([1 2], [true false true false true false])
    ans = 
        [8]    [6]    [10]
        [3]    [7]    [14]
    

    whereas using curly braces would yield the comma-separated list

    >> C{[1 2], [true false true false true false]}
    ans =
         8
    ans =
         3
    ans =
         6
    ans =
         7
    ans =
        10
    ans =
        14 
    

    Take-away message / TL;DR

    Logical and linear indexing are not exclusive types of indexing. Rather, they are two independent features of indexing. "Logical" refers to the type of the index values, and "linear" indicates that several dimensions are being collapsed and indexed as one. Both features can happen simultaneously.

    这篇关于线性索引,索引的逻辑,和所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆