为什么没有值的 (:) 表现不同?
给出以下示例:
>> I=[2 1 3;3 2 4]
I =
2 1 3
3 2 4
>> I(:)
ans =
2
3
1
2
3
4
>> I(1:2)
ans =
2 3
为什么 I(:)
返回列向量,而 I(1:2)
返回较短的行向量?
Given the following example:
>> I=[2 1 3;3 2 4]
I =
2 1 3
3 2 4
>> I(:)
ans =
2
3
1
2
3
4
>> I(1:2)
ans =
2 3
Why does I(:)
return a column vector while I(1:2)
returns a shorter row vector?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(:)
语法用作方程右侧的索引时,是一个 特殊操作,将任意维度的整个矩阵重塑为单个列向量。因此,以下两行代码给出相同的结果:当数值包含在单个冒号的两侧时,它表示 线性索引到数组中。因此,
I(1:2)
从I
中选择第一个和第二个元素(即第一列中的值)。要记住的一件事是,语法1:2
实际上创建了一个向量[1 2]
,因此I(1:2)
是与I([1 2])
相同。由于线性索引[1 2]
是行向量,因此返回值的形状为行向量[2 3]
。如果使用索引I([1; 2])
或I((1:2).')
,则线性索引是列向量,因此返回值将被塑造为列向量[2; 3]
。当您有多个以逗号分隔的索引时,这些索引将应用于正在索引的矩阵的不同维度。例如,
I(1:2, 1:2)
将返回 2×2 矩阵[2 1; 3 2]
。索引中的第一个1:2
应用于行,因此选择第一行和第二行。索引中的第二个1:2
应用于列,因此选择第一列和第二列。描述 冒号运算符 和 矩阵索引 应该有助于您更好地了解如何使用
:< /代码> 有效。
The
(:)
syntax, when used as an index on the right-hand side of an equation, is a special operation that reshapes an entire matrix of any dimension into a single column vector. The following two lines of code therefore give the same result:When numerical values are included on either side of a single colon, it denotes a range of linear indices into an array. So
I(1:2)
selects the first and second elements fromI
(i.e. the values in the first column). One thing to remember is that the syntax1:2
actually creates a vector[1 2]
, soI(1:2)
is the same asI([1 2])
. Since the linear index[1 2]
is a row vector, the returned values are shaped as a row vector[2 3]
. If you use the indexI([1; 2])
orI((1:2).')
, the linear index is a column vector, so the returned values will be shaped as a column vector[2; 3]
.When you have multiple indices separated by commas, the indices are applied to different dimensions of the matrix being indexed. For example,
I(1:2, 1:2)
will return the 2-by-2 matrix[2 1; 3 2]
. The first1:2
in the index is applied to the rows, so rows one and two are selected. The second1:2
in the index is applied to the columns, so columns one and two are selected.The MATLAB documentation describing the colon operator and matrix indexing should help give you a better understanding of how to use
:
effectively.Matlab 索引示例
访问 i 行 j 列的条目:
访问 i 行的所有项目:
访问 j 列的所有项目:
访问 i 行 j 列的条目,将 M 视为 a向量:
访问从第 i 行到第 j 行以及从第 p 列到第 q 列的子矩阵:
访问整个矩阵(冗余):
说明
冒号运算符要么给出索引范围(1:2 是范围 1 到 2(含)内的索引,而 3:5 给出范围 3、4、5),或者如果没有范围,则给出给定维度的整个范围已指定。
这与仅使用单个索引对矩阵进行索引这一事实相结合,为您提供了通过逐步执行多个条目(向下移动,增加列并重置最后一行之后的行)而产生的条目,而不是给出您只需指定的行/列即可得出您的观察结果。
Examples of Matlab Indexing
Accessing entry at row i, column j:
Accessing all items on row i:
Accessing all items on column j:
Accessing entry at row i, column j, treating M as a vector:
Accessing the sub-matrix from row i to row j and from column p to column q:
Accessing the entire matrix (redundant):
Explanation
The colon operator either gives a range of indices (1:2 is the indices in range 1 to 2, inclusive, while 3:5 gives the range 3, 4, 5) or it gives the entire range for the given dimension if no range is specified.
This, in conjunction to the fact that indexing a matrix with just a single index gives you the entry that would result from stepping through that many entries (going down the rows, incrementing the column and resetting the row after the last row) instead of giving you just the specified row/column leads to your observations.
(:)
沿列向量化矩阵,即沿列读取的元素被连接成单个列向量。a:b:c
返回从a
到c
的数字序列,增量为b
。如果省略b
,则默认设置为1
。序列
a:b:c
可用于沿列线性索引矩阵。如果用于索引多维数组,则它将选择沿该维度的元素。例如返回由
I
的行1
和列2:3
组成的矩阵,即[1 3]
另外,我们可以以任何方式得到一个索引,并用它来索引 I。
上面以列主序(沿着列)显示前三个元素,即
[2 ; 3 ; 1]
(:)
vectorizes a matrix along the columns, i.e. the elements are read along the columns are concatenated into a single column vector.a:b:c
returns a sequence of numbers froma
toc
with increments ofb
. Ifb
is omitted, it is by default set to1
.The sequence
a:b:c
can be used to index a matrix linearly along the column. If used to index a multidimensional array then it selects elements along that dimension. For e.g.returns a matrix formed by rows
1
and columns2:3
ofI
, i.e.[1 3]
Also, we can arrive at an index in any manner, and use it to index I.
The above displays the first three elements in column-major order (along the columns), i.e.
[2 ; 3 ; 1]