Matlab/Octave 1-of-K表示 [英] Matlab/Octave 1-of-K representation
本文介绍了Matlab/Octave 1-of-K表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的y的大小为5000,1(矩阵),其中包含1到10之间的整数.我想将这些索引扩展为10的1个向量.即y包含1,2,3 ...,我希望它扩展"为:
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
做到这一点的最佳方法是什么?
我尝试过:
Y = zeros(5000,10); Y(y) = 1;
但是没有用.
虽然它适用于矢量:
如果y = [2 5 7]
和Y = zeros(1,10)
,则Y(y) = [0 1 0 0 1 0 1 0 0 0]
.
解决方案
n=5
Y = ceil(10*rand(n,1))
Yexp = zeros(n,10);
Yexp(sub2ind(size(Yexp),1:n,Y')) = 1
另外,考虑使用稀疏算法,例如:创建指标矩阵.. >
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 to "expand" to:
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
What is the best way to do that?
I tried:
Y = zeros(5000,10); Y(y) = 1;
but it didn't work.
It works for vectors though:
if y = [2 5 7]
, and Y = zeros(1,10)
, then Y(y) = [0 1 0 0 1 0 1 0 0 0]
.
解决方案
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.
这篇关于Matlab/Octave 1-of-K表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文