MATLAB中这个索引是什么意思?
What the meaning of this index in MATLAB?
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)
returns 错误但 data(1,1:10)
正常。
那么这里发生了什么?
我该如何理解这个机制?
data(:, 5:4:end)
将访问 data
第一维中的所有元素,从索引 5 开始,每隔 4 个索引,直到 data
第二维中的最后一个索引。这种索引技术的语法可以这样解释:
data(startIndex:step:endIndex)
如果 data
的维度比您用于索引的维度多,则此后的每个维度都会假设 :
。
>> 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)
总结一下我的问题:
data=reshape(1:24,2,3,4)
data(:,:,1) =
1 3 5
2 4 6
data(:,:,2) =
7 9 11
8 10 12
data(:,:,3) =
13 15 17
14 16 18
data(:,:,4) =
19 21 23
20 22 24
使用这个例子你可以知道 Matlab 在做什么:
data(:,1)
ans =
1
2
data(:,12)
ans =
23
24
data(:,[1,12])
ans =
1 23
2 24
data(:,5:4:end)
ans =
9 17
10 18
如果您使用 data(:,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)
returns 错误但 data(1,1:10)
正常。
那么这里发生了什么?
我该如何理解这个机制?
data(:, 5:4:end)
将访问 data
第一维中的所有元素,从索引 5 开始,每隔 4 个索引,直到 data
第二维中的最后一个索引。这种索引技术的语法可以这样解释:
data(startIndex:step:endIndex)
如果 data
的维度比您用于索引的维度多,则此后的每个维度都会假设 :
。
>> 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)
总结一下我的问题:
data=reshape(1:24,2,3,4)
data(:,:,1) =
1 3 5 2 4 6
data(:,:,2) =
7 9 11 8 10 12
data(:,:,3) =
13 15 17 14 16 18
data(:,:,4) =
19 21 23 20 22 24
使用这个例子你可以知道 Matlab 在做什么:
data(:,1)
ans =
1 2
data(:,12)
ans =
23 24
data(:,[1,12])
ans =
1 23 2 24
data(:,5:4:end)
ans =
9 17 10 18
如果您使用 data(:,13)
,它会抛出一个错误。