这个索引在MATLAB中的含义是什么?

这个索引在MATLAB中的含义是什么?,matlab,octave,Matlab,Octave,我测试了一些指标,例如: 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) 那么数据(:,5:4:end)的含义是什么呢 我测试了一些其他指标,例如: data = reshape(1:21504,[256,4,21]); data(:,5:4:end) data(:,5:4:end) ~=

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

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)
那么
数据(:,5:4:end)
的含义是什么呢

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

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(1,1:10,1)
返回错误,但
data(1,1:10)
是正常的

那么这里发生了什么事?
如何理解此机制?

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

data(1,1) == data(1,1,1)
data(1,1:3) == data(1,1:3,1)
如果
数据
的维度比用于索引的维度多,则这将假定此后每个维度都有

数据(:,5:4:end)
将访问
数据第一维度中的所有元素
,从索引5开始,每四个索引一次,直到
数据第二维度中的最后一个索引
。这种索引技术的语法可以解释如下:

data(1,1) == data(1,1,1)
data(1,1:3) == data(1,1:3,1)
data(startIndex:step:endIndex)
如果
数据
的维度比用于索引的维度多,则将假定此后的每个维度都有

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

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

线性索引 (抄自)

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

另一方面,数据(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
数据(:,:,1)=

数据(:,:,2)=

数据(:,:,3)=

数据(:,:,4)=

使用此示例,您可以了解Matlab在做什么:

数据(:,1)

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
数据(:,12)

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
数据(:,[1,12])

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
数据(:,5:4:结束)

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
如果您使用
数据(:,13)
,它会抛出一个错误。

总结一下我的问题:

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
数据(:,:,1)=

数据(:,:,2)=

数据(:,:,3)=

数据(:,:,4)=

使用此示例,您可以了解Matlab在做什么:

数据(:,1)

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
数据(:,12)

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
数据(:,[1,12])

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24
数据(:,5:4:结束)

ans=

19    21    23
20    22    24
 1
 2
23
24
 1    23
 2    24

如果使用
数据(:,13)
,则会抛出错误。

中有一些示例