Main Content

vec2ind

Convert vectors to indices

Description

[ind,N] = vec2ind(vec) takes a matrix of vectors, each containing a single 1 and returns the indices of the ones, ind, and the number of rows in vec, N.

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

example

Examples

collapse all

This example shows how to convert three vectors to indices and back, using both the ind2vec and vec2ind functions.

Define three vector with all zeros in the last row and convert it to indices.

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

n =
     4

Convert the indices to vector, while preserving the number of rows.

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

Input Arguments

collapse all

Vector representation of the indices, specified as a matrix of vectors, each containing a single 1.

Output Arguments

collapse all

Indices, returned as a row vector.

Number of rows of the input matrix, returned as a scalar.

Version History

Introduced before R2006a

See Also

| |