matlab 矩阵元素表示方法转换 A(a) to A(x,y)
2011-10-21 20:56
330 查看
linearInd =
sub2ind(matrixSize, rowSub, colSub) returns
the linear index equivalents to the row and column subscripts rowSub andcolSub for
a matrix of size matrixSize.
The matrixSize input
is a 2-element vector that specifies the number of rows and columns in the matrix as [nRows, nCols].
The rowSub and colSub inputs
are positive, whole number scalars or vectors that specify one or more row-column subscript pairs for the matrix. Example
3 demonstrates the use of vectors for the rowSub and colSub inputs.
Examples
Example 1
This example converts the subscripts (2, 1, 2) for three-dimensional array A to a single linear index. Start bycreating a 3-by-4-by-2 array A:
rand('state', 0); % Initialize random number generator. A = rand(3, 4, 2) A(:,:,1) = 0.9501 0.4860 0.4565 0.4447 0.2311 0.8913 0.0185 0.6154 0.6068 0.7621 0.8214 0.7919 A(:,:,2) = 0.9218 0.4057 0.4103 0.3529 0.7382 0.9355 0.8936 0.8132 0.1763 0.9169 0.0579 0.0099
Find the linear index corresponding to (2, 1, 2):
linearInd = sub2ind(size(A), 2, 1, 2) linearInd = 14
Make sure that these agree:
A(2, 1, 2) A(14) ans = and = 0.7382 0.7382
Example 2
Using the 3-dimensional array A defined in the previous example, specify only 2 of the 3 subscript arguments inthe call to sub2ind. The third subscript argument defaults to 1.
The command
linearInd = sub2ind(size(A), 2, 4) ans = 11
is the same as
linearInd = sub2ind(size(A), 2, 4, 1) ans = 11
Example 3
Using the same 3-dimensional input array A as in Example 1, accomplish the work of five separate sub2ind commandswith just one.
Replace the following commands:
sub2ind(size(A), 3, 3, 2); sub2ind(size(A), 2, 4, 1); sub2ind(size(A), 3, 1, 2); sub2ind(size(A), 1, 3, 2); sub2ind(size(A), 2, 4, 1);
with a single command:
sub2ind(size(A), [3 2 3 1 2], [3 4 1 3 4], [2 1 2 2 1]) ans = 21 11 15 19 11
Verify that these linear indices access the same array elements as their subscripted counterparts:
[A(3,3,2), A(2,4,1), A(3,1,2), A(1,3,2), A(2,4,1)] ans = 0.0579 0.6154 0.1763 0.4103 0.6154 A([21, 11, 15, 19, 11]) ans = 0.0579 0.6154 0.1763 0.4103 0.6154
相关文章推荐
- 关于Matlab中矩阵元素的表示方法 .
- 关于Matlab中矩阵元素的表示方法
- 在Matlab中,想要将两个字符串连接在一起,有以下的方法: 假定有两个字符串 >> str1='Iloveyou';str2='123'; 方法一:用中括号将str1和str2像矩阵元素一样包含起来
- MATLAB中,可以用于计算数组或者矩阵元素总个数的方法
- Matlab中统计矩阵中相同元素的个数的方法
- Matlab中统计矩阵中相同元素的个数的方法
- matlab中元胞数组cell元素的表示方法
- MATLAB中对矩阵元素操作的for循环优化方法
- Matlab - 矩阵元素引用
- C#部分---arraylist集合、arraylist集合中的object数据转换成int类string类等;间隔时间的表示方法;
- javascript实现的字符串与十六进制表示字符串相互转换方法
- python实现将英文单词表示的数字转换成阿拉伯数字的方法
- Android与IOS的Transform图形变换矩阵之间快速转换方法
- matlab中如何从一个矩阵的行列中找出 0 元素的个数 或者位置 并作为判断条件
- MATLAB中查找两个不同维度的矩阵中相同或者不同元素的位置
- matlab中矩阵元素求和、求期望和均方差
- Python的numpy库中将矩阵转换为列表等函数的方法
- MATLAB矩阵元素的删除命令
- UNIX常用的IP表示掩码方式转换为字符串的程序实现方法
- matlab 元胞matrix{n},每个元素可以代表不同维数的矩阵