Main Content

ind2vec

Convert indices to vectors

Description

vec = ind2vec(ind) takes a row vector of indices, ind, and returns a sparse matrix of vectors, vec, containing a 1 in the row of the index they represent, as indicated by ind.

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

vec = ind2vec(ind,N) returns an N-by-M sparse matrix, where N can be equal to or greater than the maximum index.

example

Examples

collapse all

This example shows how to convert indices to vector representation using the ind2vec function.

Define four indices and then convert them to vector representation.

ind = [1 3 2 3];
vec = ind2vec(ind)
vec =
   (1,1)        1
   (3,2)        1
   (2,3)        1
   (3,4)        1

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

Define a 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

Indices, specified as a row vector.

Number of rows of the output matrix, specified as a scalar.

Output Arguments

collapse all

Vector representation of the indices, returned as an N-by-M sparse matrix.

Version History

Introduced before R2006a

See Also

| |