I've a label vector as follows:
y=[3 1 5 3 4 2]; Is there any efficient way to generate the following label matrix?
[0 0 1 0 0; 1 0 0 0 0; 0 0 0 0 1; 0 0 1 0 0; 0 0 0 1 0; 0 1 0 0 0;] UPDATED: This post and this post are both good answers. Using the scripts provided by @Nras, the following is to handle the missing labels:
Y=[3 1 5 3 4 2]; labels=unique(Y); [~,indexes]=ismember(Y,labels); rows = 1:length(Y); %// row indx T = zeros(length(Y),length(unique(indexes))); %// A matrix full of zeros T(sub2ind(size(T),rows ,indexes)) = 1; %// Ones at the desired row/column combinations