【问题标题】:What the meaning of this index in MATLAB?这个索引在 MATLAB 中的含义是什么?
【发布时间】:2016-08-18 09:33:13
【问题描述】:
data = reshape(1:21504,[256,4,21]);
data(:,5:4:end)

我测试了一些指标,比如:

data(:,5:4:end) ~= data(:,5:4:end,1)
data(:,5:4:end) ~= data(:,1,5:4:end)

那么data(:,5:4:end)是什么意思呢?

我测试了一些其他的指标,比如:

data(1,1) == data(1,1,1)
data(1,1:3) == data(1,1:3,1)

并发现一些奇怪的行为,例如data(1,1:10,1)返回错误但data(1,1:10)是可以的。

那么这里发生了什么?
我如何理解这种机制?

【问题讨论】:

标签: matlab octave


【解决方案1】:
>> size(data)
ans =
256     4    21

data(1,1:10,1) 从第一行中选择第 1-10 列(所有三个维度均已明确设置),但只有 4 列。因此错误。

另一方面,data(1,1:10) 使用线性索引,它将维度 2 和 3 解释为一长串值并选择其前 10 个值。

线性索引

What does this expression A(14) do?

When you index into the matrix A using only one subscript, MATLAB treats A as if its elements were strung out in a long column vector, by going down the columns consecutively, as in:

          16
           5
           9
         ...
           8
          12
           1

The expression A(14) simply extracts the 14th element of the implicit column vector. Indexing into a matrix with a single subscript in this way is often called linear indexing.

Here are the elements of the matrix A along with their linear indices:
matrix_with_linear_indices.gif

The linear index of each element is shown in the upper left.

From the diagram you can see that A(14) is the same as A(2,4).

The single subscript can be a vector containing more than one linear index, as in:

  A([6 12 15])
  ans =
      11   15   12

Consider again the problem of extracting just the (2,1), (3,2), and (4,4) elements of A. You can use linear indexing to extract those elements:

  A([2 7 16])
  ans =
      5   7   1

That's easy to see for this example, but how do you compute linear indices in general? MATLAB provides a function called sub2ind that converts from row and column subscripts to linear indices. You can use it to extract the desired elements this way:

  idx = sub2ind(size(A), [2 3 4], [1 2 4])
  ans =
      2   7   16
  A(idx)
  ans =
      5   7   1

(复制自http://de.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html

【讨论】:

    【解决方案2】:

    data(:, 5:4:end) 将访问data 的第一维中的所有元素,并从索引 5 开始,每隔 4 个索引,直到data 第二维中的最后一个索引。这种索引技术的语法可以这样解释:

    data(startIndex:step:endIndex)
    

    如果data 的维度比您用于索引的维度多,则这将假定: 用于之后的每个维度。

    【讨论】:

      【解决方案3】:

      总结一下我的问题:

      data=reshape(1:24,2,3,4)
      

      数据(:,:,1) =

       1     3     5
       2     4     6
      

      数据(:,:,2) =

       7     9    11
       8    10    12
      

      数据(:,:,3) =

      13    15    17
      14    16    18
      

      数据(:,:,4) =

      19    21    23
      20    22    24
      

      通过这个例子你可以知道Matlab在做什么:

      数据(:,1)

      ans =

       1
       2
      

      数据(:,12)

      ans =

      23
      24
      

      数据(:,[1,12])

      ans =

       1    23
       2    24
      

      数据(:,5:4:end)

      ans =

       9    17
      10    18
      

      如果你使用data(:,13),它会抛出一个错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-26
        • 2020-05-21
        相关资源
        最近更新 更多