vec2ind

Convert vectors to indices

Syntax

[ind,n] = vec2ind

Description

ind2vec and vec2ind(vec) allow indices to be represented either by themselves or as vectors containing a 1 in the row of the index they represent.

[ind,n] = vec2ind takes one argument,

vecMatrix of vectors, each containing a single 1

and returns

indThe indices of the 1s
nThe number of rows in vec

Examples

Here three vectors are converted to indices and back, while preserving the number of rows.

vec = [0 0 1 0; 1 0 0 0; 0 1 0 0]'
vec =
     0     1     0
     0     0     1
     1     0     0
     0     0     0
[ind,n] = vec2ind(vec)
ind =
     3     1     2

n =
     4
vec2 = full(ind2vec(ind,n)) 
vec2 =
     0     1     0
     0     0     1
     1     0     0
     0     0     0

See Also

| |

Introduced before R2006a