Contents

ind2vec

Convert indices to vectors

Syntax

ind2vec(ind)
ind2vec(ind,N)

Description

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.

ind2vec(ind) takes one argument,

indRow vector of indices

and returns a sparse matrix of vectors, with one 1 in each column, as indicated by ind.

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

Examples

Here four indices are defined and converted to vector representation.

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

Here a vector with all zeros in the last row is 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

| |

Was this topic helpful?