Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert vector of indices into matrix

Tags:

matlab

I want to convert a vector of indices into a matrix with ones in the columns of the indices.

x = [2;1;3;1];
m = someFunc(x,3)
% m =
%
%   0   1   0
%   1   0   0
%   0   0   1
%   1   0   0
like image 376
Jacob Eggers Avatar asked Mar 30 '12 17:03

Jacob Eggers


People also ask

What is vec2ind in MATLAB?

[ 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.

What does ind2sub mean in MATLAB?

Description. example. [ row , col ] = ind2sub( sz , ind ) returns the arrays row and col containing the equivalent row and column subscripts corresponding to the linear indices ind for a matrix of size sz .

Can we convert matrix to vector?

vector() Functions. So far, we have converted our matrix by columns. However, it is also possible to convert a matrix to a one-dimensional vector by rows.


2 Answers

I tested the sub2ind function, but on the coursera Machine Learning forum I was pointed to this beauty.

m = eye(num_cols)(x,:);

It uses the identity matrix to select the appropriate column based on the value in x.

like image 117
Hans Then Avatar answered Oct 12 '22 23:10

Hans Then


One way is to use SUB2IND function:

colN = 3;
assert(max(x)<=colN,'Not enough columns') %# check that you have enough columns
%# other checks that x is valid indices

m = zeros(numel(x),colN);
m(sub2ind(size(m),1:numel(x),x')) = 1;
like image 24
yuk Avatar answered Oct 12 '22 22:10

yuk