SUB2IND

Purpose:

Converts row-column subscripts to linear (unraveled) indices.

Syntax:

SUB2IND(nr, nc, np, row, col, page)

nr

-

An integer. The number of rows.

nc

-

An integer. The number of columns.

np

-

Optional. An integer, the number of 3D pages. Defaults to 1.

row

-

A series, the row indices.

col

-

A series, the column indices.

page

-

Optional. A series, the 3D page indices. Defaults to 1.

 

Alternate Syntax:

SUB2IND(sz, row, col, page)

sz

-

A series, where the values specify the number of rows, columns and optional pages.

row

-

A series, the row indices.

col

-

A series, the column indices.

page

-

Optional. A series, the 3D page indices. Defaults to 1.

Returns:

An integer series of linear array indices.

Example:

W1: magic(10)

W2: {W1[6, 3]}

W3: {sub2ind(numrows(w1), numcols(w1), 6, 3)}

W4: w1[w3]

 

W1 contains a 10x10 magic array where the sum of each column is equal to the sum of each row.

 

W2 returns {76}, the value of W1 at row 6, column 3.

 

W3 converts the array subscripts 6, 3 to the linear array index 26.

 

W4 returns {76}, the 26th value of W1, the same as the value at row 6, column 3.

Example:

W1: magic(10)

W2: {W1[6, 3]}

W3: {sub2ind(size(w1), 6, 3)}

W4: w1[w3]

 

Same as above, except SIZE is used in W3 to specify the number of rows and columns.

Example:

W1: {{10, 20, 30},

     {11, 21, 31},

     {12, 22, 33}}

W2: {w1[1, 1], w1[2, 2], w1[3, 3]}

W3: sub2ind(numrows(w1), numcols(w1), {1, 2, 3}, {1, 2, 3})

W4: W1[w3]

W5: diag(w1)

 

W1 contains a 3x3 array.

 

W2 contains the series {10, 21, 33}, the main diagonal of W1 by explicitly returning the values at row 1, column 1, row 2, column 2, and row 3, column 3.

 

W3 produces the linear indices {1, 5, 9} of the diagonal elements of W1 from the diagonal subscripts, (1, 1), (2, 2), (3, 3).

 

W4 uses the linear indices in W3 to produce the main diagonal of W1, {10, 21, 33}.

 

W5 uses DIAGONAL to produce the values of the main diagonal of W1, {10, 21, 33}.

Example:

W1: {{10, 20, 30},

     {11, 21, 31},

     {12, 22, 33}}

W2: {w1[1, 1], w1[2, 2], w1[3, 3]}

W3: sub2ind(size(w1), {1, 2, 3}, {1, 2, 3})

W4: W1[w3]

W5: diag(w1)

 

Same as above, except SIZE is used in W3 to specify the number of rows and columns.

Example:

W1: 10 * ravel(1..100, 5);table

W2: {sub2ind(5, 2, 10, 3, 2, 4)}

W3: W1[w2]

W4: ind2sub(5, 2, 10, W2)

 

W1 contains a 5x20 array.

 

W2 considers W1 to be a 3D array of size 5x2x10. The returned value of 38 is the linear address of the element at row 3, column 2, page 4.

 

W3 contains 280, the value at element 38. This is the same as the value at subscript (3, 2, 4).

 

W4 contains the series {{3, 2, 4}}, the original subscripts row 3, column 2, page 4.

Remarks:

SUB2IND converts array subscripts in row column form to linear or unraveled indices.

 

See IND2SUB to convert linear or unraveled array indices to subscripts in row column form.

See Also:

REORDER

IDXTODT

IDXTOX

IND2SUB

XTODT

XTOIDX