Matlab/Octave 1-of-K representation

前端 未结 5 1788
北恋
北恋 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:51

    n=5
    Y = ceil(10*rand(n,1))
    Yexp = zeros(n,10);
    Yexp(sub2ind(size(Yexp),1:n,Y')) = 1
    

    Also, consider using sparse, as in: Creating Indicator Matrix.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题