MATLAB Function Reference | ![]() ![]() |
Syntax
IND = sub2ind(siz
,I,J) IND = sub2ind(siz
,I1,I2,...,In)
Description
The sub2ind
command determines the equivalent single index corresponding to a set of subscript values.
IND = sub2ind(
returns the linear index equivalent to the row and column subscripts siz
,I,J)
I
and J
for a matrix of size siz
.
IND = sub2ind(
returns the linear index equivalent to the siz
,I1,I2,...,In)
n
subscripts I1
,I2
,...,In
for an array of size siz
.
Examples
Create a 3-by-4-by-2 matrix, A
.
A = [17 24 1 8; 2 22 7 14; 4 6 13 20]; A(:,:,2) = A - 10 A(:,:,1) = 17 24 1 8 2 22 7 14 4 6 13 20 A(:,:,2) = 7 14 -9 -2 -8 12 -3 4 -6 -4 3 10
The value at row 2, column 1, page 2 of the matrix is -8.
A(2,1,2) ans = -8
To convert A(2,1,2)
into its equivalent single subscript, use sub2ind
.
sub2ind(size(A),2,1,2) ans = 14
You can now access the same location in A
using the single subscripting method.
A(14) ans = -8
See Also
![]() | strvcat | subplot | ![]() |