如何在 Matlab 中选择子矩阵(不是任何特定模式) [英] How to select a submatrix (not in any particular pattern) in Matlab

查看:40
本文介绍了如何在 Matlab 中选择子矩阵(不是任何特定模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Matlab 中选择一个子矩阵(不是任何模式)?例如,对于一个大小为10×10的矩阵,如何选取第1、2、9行与第4、6列的交集组成的子矩阵?

感谢您提供任何有用的答案!

解决方案

TLDR:简答

至于您的问题,假设您有一个任意的 10×10 矩阵 A.提取所需子矩阵的最简单方法是使用索引向量:

B = A([1 2 9], [4 6]);


MATLAB 中的索引

官方文档中有一篇有趣的文章解释了 MATLAB 中的索引.基本上,有几种方法可以提取值的子集,我将为您总结一下:

1.索引向量

索引向量表示要提取的元素的索引.它们可以包含单个索引或多个索引,如下所示:

A = [10 20 30 40 50 60 70 80 90]%# 提取第三个和第九个元素B = A([3 9]) %# B = [30 90]

可以为每个维度单独指定索引向量,例如:

A = [10 20 30;40 50 60;70 80 90];%# 提取第一行和第三行,以及第一列和第二列B = A([1 3], [1 2]) %# B = [10 30;40 60]

还有两个特殊的下标:end 和冒号 (:):

  • end 仅表示该维度中的最后一个索引.
  • 冒号只是1:end"的简写.

例如,您可以编写A(:, 2:end),而不是编写A([1 2 3], [2 3]).这对于大型矩阵尤其有用.

2.线性索引

线性索引通过将列连接成一个列向量并分别为元素分配索引,将任何矩阵视为列向量.例如,我们有:

A = [10 20 30;40 50 60;70 80 90];

我们想要计算 b = A(2).等效列向量为:

A = [10;40;70;20;50;80;30;60;90]

因此 b 等于 40.

当然也允许使用特殊的冒号和 end 下标.因此,A(:) 将任何矩阵 A 转换为列向量.

带有矩阵下标的线性索引:也可以使用另一个矩阵进行线性索引.下标矩阵被简单地转换为列向量,并用于线性索引.然而,结果矩阵始终与下标矩阵具有相同的维度.
例如,如果 I = [1 3;1 2],那么A(I)和写reshape(A(I(:)), size(I))是一样的.>

从矩阵下标转换为线性索引,反之亦然:为此,您有 sub2indind2sub.比如想把矩阵A(对应元素30)中的下标[1, 3]转换成线性索引,可以写sub2ind(size(A), 1, 3)(本例中的结果当然应该是 7).

3.逻辑索引

在逻辑索引中,下标是二进制的,其中逻辑 1 表示选择了相应的元素,0 表示没有选择.下标向量必须与原始矩阵具有相同的维度或具有相同元素数的向量.例如,如果我们有:

A = [10 20 30;40 50 60;70 80 90];

并且我们想使用逻辑索引提取A([1 3], [1 2]),我们可以这样做:

Ir = logical([1 1 0]);Ic = 逻辑([1 0 1]);B = A(Ir, Ic)

或者这个:

I = logical([1 0 1; 1 0 1; 0 0 0]);B = A(I)

或者这个:

I = logical([1 1 0 0 0 0 1 1 0]);B = A(I)

请注意,后两种情况是一维向量,如有必要,应重新整形回矩阵(例如,使用 reshape).

How to select a submatrix (not in any pattern) in Matlab? For example, for a matrix of size 10 by 10, how to select the submatrix consisting of intersection of the 1st 2nd and 9th rows and the 4th and 6th columns?

Thanks for any helpful answers!

解决方案

TLDR: Short Answer

As for your question, suppose you have an arbitrary 10-by-10 matrix A. The simplest way to extract the desired sub-matrix would be with an index vector:

B = A([1 2 9], [4 6]);


Indexing in MATLAB

There's an interesting article in the official documentation that comprehensively explains indexing in MATLAB. Basically, there are several ways to extract a subset of values, I'll summarize them for you:

1. Indexing Vectors

Indexing vectors indicate the indices of the element to be extracted. They can either contain a single index or several, like so:

A = [10 20 30 40 50 60 70 80 90]

%# Extracts the third and the ninth element
B = A([3 9])  %# B = [30 90]

Indexing vectors can be specified for each dimension separately, for instance:

A = [10 20 30; 40 50 60; 70 80 90];

%# Extract the first and third rows, and the first and second columns
B = A([1 3], [1 2])  %# B = [10 30; 40 60]

There are also two special subscripts: end and the colon (:):

  • end simply indicates the last index in that dimension.
  • The colon is just a short-hand notation for "1:end".

For example, instead of writing A([1 2 3], [2 3]), you can write A(:, 2:end). This is especially useful for large matrices.

2. Linear Indexing

Linear indexing treats any matrix as if it were a column vector by concatenating the columns into one column vector and assigning indices to the elements respectively. For instance, we have:

A = [10 20 30; 40 50 60; 70 80 90];

and we want to compute b = A(2). The equivalent column vector is:

A = [10;
     40;
     70;
     20;
     50;
     80;
     30;
     60;
     90]

and thus b equals 40.

The special colon and end subscripts are also allowed, of course. For that reason, A(:) converts any matrix A into a column vector.

Linear indexing with matrix subscripts: It is also possible to use another matrix for linear indexing. The subscript matrix is simply converted into a column vector, and used for linear indexing. The resulting matrix is, however always of the same dimensions as the subscript matrix.
For instance, if I = [1 3; 1 2], then A(I) is the same as writing reshape(A(I(:)), size(I)).

Converting from matrix subscripts to linear indices and vice versa: For that you have sub2ind and ind2sub, respectively. For example, if you want to convert the subscripts [1, 3] in matrix A (corresponding to element 30) into a linear index, you can write sub2ind(size(A), 1, 3) (the result in this case should be 7, of course).

3. Logical Indexing

In logical indexing the subscripts are binary, where a logical 1 indicates that the corresponding element is selected, and 0 means it is not. The subscript vector must be either of the same dimensions as the original matrix or a vector with the same number of elements. For instance, if we have:

A = [10 20 30; 40 50 60; 70 80 90];

and we want to extract A([1 3], [1 2]) using logical indexing, we can do either this:

Ir = logical([1 1 0]);
Ic = logical([1 0 1]);
B = A(Ir, Ic)

or this:

I = logical([1 0 1; 1 0 1; 0 0 0]);
B = A(I)

or this:

I = logical([1 1 0 0 0 0 1 1 0]);
B = A(I)

Note that in the latter two cases is a one-dimensional vector, and should be reshaped back into a matrix if necessary (for example, using reshape).

这篇关于如何在 Matlab 中选择子矩阵(不是任何特定模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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