在 Matlab 中与由矩阵索引的向量混淆 [英] Got confused with a vector indexed by a matrix, in Matlab

查看:42
本文介绍了在 Matlab 中与由矩阵索引的向量混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在 Matlab 中运行:

The following codes runs in Matlab:

a = [1 2 3 4]
b = [ 1 2 3; 1 2 3; 1 2 3]
a(b)

a(b) 的结果是一个矩阵:

The result of a(b) is a matrix:

[ 1 2 3; 1 2 3; 1 2 3]

谁能解释一下这里发生了什么?为什么向量可以被矩阵索引,如何解释结果?

Can anyone explain what happened here? Why a vector can be indexed by a matrix, how to interpret the result?

推荐答案

这是您正在执行的非常标准的 MATLAB 操作.当您有一个向量或矩阵时,您可以提供另一个向量或矩阵以访问特定值.在 MATLAB 中访问值不仅限于单个索引(即 A(1)、A(2) 等).

That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. A(1), A(2) and so on).

例如,您所拥有的是 a = [1 2 3 4] 的向量.当您尝试使用 b 访问向量时,您实际上在做的是查找.输出与b的大小基本相同,你所做的是创建一个矩阵,其中有3行,每个元素访问第一个、第二个和第三个元素.您不仅可以对向量执行此操作,还可以对矩阵执行此操作.

For example, what you have there is a vector of a = [1 2 3 4]. When you try to use b to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and what you are doing is creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well.

请记住,当您为矩阵执行此操作时,您将访问列主要格式中的元素.例如,假设我们有这个矩阵:

Bear in mind that when you're doing this for a matrix, you access the elements in column major format. For example, supposing we had this matrix:

 A = [1 2
      3 4
      5 6
      7 8]

A(1) 为 1,A(2) 为 3,A(3) 为 5,依此类推.您将从第一列开始,递增的索引将向下遍历第一列.一旦你找到第 5 个索引,它就会跳到下一列.所以 A(5) 将是 2,A(6) 将是 4,依此类推.

A(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So A(5) would be 2, A(6) would be 4 and so on.

以下是一些示例,可进一步加深您的理解.让我们定义一个矩阵 A 使得:

Here are some examples to further your understanding. Let's define a matrix A such that:

 A = [5 1 3
      7 8 0
      4 6 2]

以下是一些 MATLAB 代码,可加强您对此类索引的理解:

Here is some MATLAB code to strengthen your understanding for this kind of indexing:

 A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
 B = [1 2 3 4];
 C = A(B); % C should give [5 7 4 1]
 D = [5 6 7; 1 2 3; 4 5 6];
 E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
 F = [9 8; 7 6; 1 2];
 G = A(F); % G should give [2 0; 3 6; 5 7]

因此,以这种方式访问​​元素时的输出是您指定为参数的向量或矩阵的大小.

As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.

为了完整起见,让我们对向量执行此操作:

In order to be complete, let's do this for a vector:

 V = [-1 9 7 3 0 5]; % A 6 x 1 vector
 B = [1 2 3 4];
 C = V(B); % C should give [-1 9 7 3]
 D = [1 3 5 2];
 E = V(D); % E should give [-1 7 0 9]
 F = [1 2; 4 5; 6 3];
 G = V(F); % G should give [-1 9; 3 0; 5 7]

注意:您必须确保没有提供会使访问越界的索引.例如,如果您尝试在示例中指定索引 5,则会出现错误.此外,如果您在我的示例中尝试了大于 9 的任何值,它也会给您一个错误.在那个 3 x 3 矩阵中有 9 个元素,因此指定大于 9 的任何列主索引会给你一个越界错误.

NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.

这篇关于在 Matlab 中与由矩阵索引的向量混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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