线性索引、逻辑索引等等 [英] Linear indexing, logical indexing, and all that

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

问题描述

我们习惯了 Matlab 中不同形式的索引:

We are used to different forms of indexing in Matlab:

  • 标准(沿每个维度使用整数),
  • 逻辑(使用逻辑值),
  • 线性(使用单个索引遍历多维数组).

乍一看,这些形式似乎是排他的:索引要么是标准的,要么是逻辑的,要么是线性的.然而,有时这些形式中的几种似乎是混合的.例如,

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

这是逻辑索引,对吧?但是它也有一些线性索引的特性,因为返回的是一个列向量.实际上,逻辑索引A>5和线性索引find(A>5)的作用是一样的.

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).

作为第二个例子,考虑

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

  • Matlab 中有哪些类型的索引?
  • 如何将它们结合起来?
  • 应该如何提及它们?

推荐答案

在下面我使用术语,我认为这或多或少符合标准的 Matlab 实践.然而,在某些情况下,我不得不编造一个名字,因为我不知道一个现有的名字.如果有比我使用的更多的标准名称,请告诉我.

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.

这个答案试图阐明不同类型的索引以及它们如何组合.另一个问题是如何将输出数组的形状(大小)确定为索引变量形状的函数.一篇关于此的好帖子是索引的本质 作者:Loren Shure.

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.

索引可以根据以下两个属性进行分类.

Indexing can be classified considering the following two attributes.

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

  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:

  • 多维索引为数组的每个维度指定一个索引变量.个别索引有时在 Matlab 文档中称为 下标(参见例如 sub2ind).
  • 线性索引指定了一个索引变量,它在所有维度上遍历数组(这可以被视为所有维度都合并为一个).正如我们所知,遍历首先是沿着列,然后是行,然后是第三个维度的切片,等等(所谓的 列主序).
  • 部分线性索引:给定一个m+n维的数组,n>=2,可以指定mm 维的索引变量(因此在这些维度中使用多维索引)和最后 n 维的一个索引变量,它被解释为线性索引仅适用于这些维度(最后一个 n 维度合并为一个).
  • 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).

根据索引值的类型,每个索引变量可以是整数值或逻辑值:

Acccording to the type of the index values, each index variable can be integer-valued or logical:

  • 如果索引变量包含正整数,则它是整数值
  • 如果索引变量包含逻辑值,则它是逻辑.

分类标准 1 和 2 独立.指标1的类别与指标2的类别没有关系,所有组合都是可能的.

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.

因此,根据上述分类,索引有6种基本类型.为了澄清,以下是每个示例.所有例子都使用数组A = cat(3, magic(3), 9+magic(3)),即

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. 多维整数值:

  1. Multidimensional, integer-valued:

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

  • 线性,整数值:

  • Linear, integer-valued:

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

  • 部分线性,整数值:

  • Partially linear, integer-valued:

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

  • 多维、逻辑:

  • 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
    

    缺失值解释为false,剩余值必须为false,否则会出错.参见例如 Mathworks 的这个页面乔纳斯的这个回答.

    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.

    线性、逻辑:

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

    (请注意,索引向量中已省略了 11 个尾随 false 值.)

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

    部分线性,逻辑:

    >> 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. 多维、逻辑/整数值:

    1. Multidimensional, logical/integer-valued:

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

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

  • 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.

    为数值数组描述的所有类型的索引都可以应用于元胞数组,但有一个额外的考虑.元胞数组可以用圆括号或花括号索引.在第一种情况下,索引的结果是一个元胞数组.第二个是逗号分隔的单元格内容列表.

    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.

    举个例子,假设前面例子中用到的数值数组转化为元胞数组C = num2cell(A),即

    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]
    

    那么上面示例 8 中使用的索引将生成元胞数组

    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 
    

    外卖信息/TL;DR

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

    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天全站免登陆