Matlab/Octave 1-of-K representation

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

    Consider the following:

    y = randi([1 10],[5 1]);       %# vector of 5 numbers in the range [1,10]
    yy = bsxfun(@eq, y, 1:10)';    %# 1-of-10 encoding
    

    Example:

    >> y'
    ans =
         8     8     4     7     2
    >> yy
    yy =
         0     0     0     0     0
         0     0     0     0     1
         0     0     0     0     0
         0     0     1     0     0
         0     0     0     0     0
         0     0     0     0     0
         0     0     0     1     0
         1     1     0     0     0
         0     0     0     0     0
         0     0     0     0     0
    

提交回复
热议问题