为什么没有值的 (:) 表现不同?

发布于 2024-08-30 07:58:59 字数 280 浏览 3 评论 0原文

给出以下示例:

>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情感失落者 2024-09-06 07:58:59

(:) 语法用作方程右侧的索引时,是一个 特殊操作,将任意维度的整个矩阵重塑为单个列向量。因此,以下两行代码给出相同的结果:

a = I(:);               % Create a column vector with ":"
a = reshape(I, [], 1);  % Create a column vector with RESHAPE

当数值包含在单个冒号的两侧时,它表示 线性索引到数组中。因此,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:

a = I(:);               % Create a column vector with ":"
a = reshape(I, [], 1);  % Create a column vector with RESHAPE

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 from I (i.e. the values in the first column). One thing to remember is that the syntax 1:2 actually creates a vector [1 2], so I(1:2) is the same as I([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 index I([1; 2]) or I((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 first 1:2 in the index is applied to the rows, so rows one and two are selected. The second 1: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.

似狗非友 2024-09-06 07:58:59

Matlab 索引示例

[rows,cols] = size(M); % M is a rows x cols matrix

访问 i 行 j 列的条目:

x = M(i,j);

访问 i 行的所有项目:

r = M(i,:);

访问 j 列的所有项目:

c = M(:,j);

访问 i 行 j 列的条目,将 M 视为 a向量:

x = M(rows*(j-1)+i);

访问从第 i 行到第 j 行以及从第 p 列到第 q 列的子矩阵:

S = M(i:j,p:q);

访问整个矩阵(冗余):

M = M(:,:);

说明
冒号运算符要么给出索引范围(1:2 是范围 1 到 2(含)内的索引,而 3:5 给出范围 3、4、5),或者如果没有范围,则给出给定维度的整个范围已指定。

这与仅使用单个索引对矩阵进行索引这一事实相结合,为您提供了通过逐步执行多个条目(向下移动,增加列并重置最后一行之后的行)而产生的条目,而不是给出您只需指定的行/列即可得出您的观察结果。

Examples of Matlab Indexing

[rows,cols] = size(M); % M is a rows x cols matrix

Accessing entry at row i, column j:

x = M(i,j);

Accessing all items on row i:

r = M(i,:);

Accessing all items on column j:

c = M(:,j);

Accessing entry at row i, column j, treating M as a vector:

x = M(rows*(j-1)+i);

Accessing the sub-matrix from row i to row j and from column p to column q:

S = M(i:j,p:q);

Accessing the entire matrix (redundant):

M = M(:,:);

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.

木有鱼丸 2024-09-06 07:58:59

(:) 沿列向量化矩阵,即沿列读取的元素被连接成单个列向量。 a:b:c 返回从 ac 的数字序列,增量为 b。如果省略 b,则默认设置为 1

序列a:b:c可用于沿列线性索引矩阵。如果用于索引多维数组,则它将选择沿该维度的元素。例如

I(1,2:3) 

返回由I的行1和列2:3组成的矩阵,即[1 3]

另外,我们可以以任何方式得到一个索引,并用它来索引 I。

index = [1 2 3];
disp(I(index));

上面以列主序(沿着列)显示前三个元素,即 [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 from a to c with increments of b. If b is omitted, it is by default set to 1.

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.

I(1,2:3) 

returns a matrix formed by rows 1 and columns 2:3 of I, i.e. [1 3]

Also, we can arrive at an index in any manner, and use it to index I.

index = [1 2 3];
disp(I(index));

The above displays the first three elements in column-major order (along the columns), i.e. [2 ; 3 ; 1]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文