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

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

问题描述

如何在Matlab中选择子矩阵(不以任何模式)?例如,对于大小为10 x 10的矩阵,如何选择由第一第二行和第9行与第四行和第六列的交集组成的子矩阵?

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:简短答案

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

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]);


官方文档中有一篇有趣的文章介绍了在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:

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

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]

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

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

  • end仅表示该维度的最后一个索引.
  • 冒号只是"1:end"的简写形式.
  • end simply indicates the last index in that dimension.
  • The colon is just a short-hand notation for "1:end".

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

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

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

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];

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

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

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

,因此b等于40.

特殊冒号和end下标.因此,A(:)会将任何矩阵A转换为列向量.

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

带有矩阵下标的线性索引: 也可以将另一个矩阵用于线性索引.下标矩阵被简单地转换为列向量,并用于线性索引.但是,所得矩阵的尺寸始终与下标矩阵相同.
例如,如果I = [1 3; 1 2],则A(I)与编写reshape(A(I(:)), size(I))相同.

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)).

从矩阵下标转换为线性索引,反之亦然: 为此,您具有 sub2ind

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).

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

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];

,并且我们想使用逻辑索引提取A([1 3], [1 2]),我们可以执行以下任一操作:

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)

或者这个:

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)

请注意,在后两种情况下是一维向量,如有必要,应将其整形为矩阵(例如,使用

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天全站免登陆