Convert vectors to indices
[ind,n] = vec2ind(vec)
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.
[ind,n] = vec2ind(vec)
takes one argument,
vec | Matrix of vectors, each containing a single 1 |
and returns
ind | The indices of the 1s |
n | The number of rows in vec |
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