iMatlab

爱学习,爱Matlab

MATLAB中矩阵的嵌套

偶然发现了别人的仿真中一段精妙的代码(哈哈,这个不分享了,变量定义了好多,不方便大家阅读),查找它的原理,分享给大家。

Using Logicals in Array Indexing
A logical array index designates(指定了) the elements of an array A based on their position in the indexing array, B, not their value. In this masking type of operation, every true element in the indexing array is treated as a positional index into the array being accessed.

In the following example, B is a matrix of logical ones and zeros. The position of these elements in B determines which elements of A are designated by the expression A(B):
A = [1 2 3; 4 5 6; 7 8 9]
A =
     1     2     3
     4     5     6
     7     8     9


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


C=A(B)

=  4

     2

     6
     9

得出的结果C是一个列向量。

很多人看见两个这种一个矩阵套在另一个矩阵的形式后就觉得不对,其实这种形式是存在的,但是注意B是类型,是一个逻辑变量构成的矩阵。

来源:爱兰松

评论

热度(1)

  1. 松兰窝松兰窝 转载了此文字  到 iMatlab