Documentation Center

  • Trial Software
  • Product Updates



Convert indices to vectors




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.


Here four indices are defined and converted to vector representation.

ind = [1 3 2 3]
vec = ind2vec(ind)

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]'
[ind,n] = vec2ind(vec)
vec2 = full(ind2vec(ind,n))

See Also

Was this topic helpful?