Matlab/Octave 1-of-K representation

前端 未结 5 1779
北恋
北恋 2021-01-05 14:58

I have a y of size 5000,1 (matrix), which contains integers between 1 and 10. I want to expand those indices into a 1-of-10 vector. I.e., y contains 1,2,3... and I want it t

5条回答
  •  北海茫月
    2021-01-05 15:43

    You can try cellfun operations:

    function vector = onehot(vector,decimal)
        vector(decimal)=1;
    end
    aa=zeros(10,2);
    dec=[5,6];
    %split into columns
    C=num2cell(aa,1);
    D=num2cell(dec,1);
    onehotmat=cellfun("onehot",C,D,"UniformOutput",false);
    output=cell2mat(onehotmat);
    

提交回复
热议问题