有人可以解释在MATLAB中从矩阵中删除元素的示例吗? [英] Can someone explain this example of deleting elements from a matrix in MATLAB?

查看:121
本文介绍了有人可以解释在MATLAB中从矩阵中删除元素的示例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的示例出现在MATLAB教程中:

The following example appears in the MATLAB tutorial:

X = [16  2 13;
     5  11  8;
     9   7 12;
     4  14  1]

使用单个下标会删除单个元素或元素序列,并将其余元素重塑为行向量.所以:

Using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector. So:

X(2:2:10) = []

导致:

X = [16 9 2 7 13 12 1]

神秘地,整个第二行和第四行中的前两个元素已被删除,但是我看不到删除的元素的位置与索引向量2:2:10之间的对应关系.有人可以解释吗?

Mysteriously, the entire 2nd row and the first two elements in the 4th row have been deleted, but I can't see the correspondence between the position of the deleted elements and the index vector 2:2:10. Can someone please explain?

推荐答案

您提供的示例显示了

The example you gave shows linear indexing. When you have a multidimensional array and you give it a single scalar or vector, it indexes along each column from top to bottom and left to right. Here's an example of indexing into each dimension:

mat = [1 4 7; ...
       2 5 8; ...
       3 6 9];
submat = mat(1:2, 1:2);

submat将包含矩阵的左上角:[1 4; 2 5].这是因为子索引中的第一个1:2访问第一个维度(行),第二个1:2访问第二个维度(列),从而提取了一个2×2的正方形.如果您没有为每个维度提供一个索引(以逗号分隔),而是仅提供一个索引,则MATLAB会将索引索引到矩阵中,就像它是一个大列向量一样:

submat will contain the top left corner of the matrix: [1 4; 2 5]. This is because the first 1:2 in the subindex accesses the first dimension (rows) and the second 1:2 accesses the second dimension (columns), extracting a 2-by-2 square. If you don't supply an index for each dimension, separated by commas, but instead just one index, MATLAB will index into the matrix as though it were one big column vector:

submat = mat(3, 3);     % "Normal" indexing: extracts element "9"
submat = mat(9);        % Linear indexing: also extracts element "9"
submat = mat([1 5 6]);  % Extracts elements "1", "5", and "6"

有关更多详细信息,请参见 MATLAB文档.

See the MATLAB documentation for more detail.

这篇关于有人可以解释在MATLAB中从矩阵中删除元素的示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆