Octave中的1-K编码 [英] 1-of-K coding in Octave

查看:222
本文介绍了Octave中的1-K编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Octave中得到一个整数向量的二进制K编码。我有一个向量 y ,比如说
$ b $ pre $ y = [1 ; 2; 3; 1; 3]

我想要一个矩阵

  Y = [1 0 0 
0 1 0
0 0 1
1 0 0
0 0 1]

我可以用

来构造一个K矩阵, pre> Y = [];
Y = [Y y == 1];
Y = [Y y == 2];
Y = [Y y == 3];

但是当我尝试用来构造循环,

  Y = []; 
for i = unique(y),
Y = [Y y == i];
endfor

出错:

 错误:mx_el_eq:不一致参数(op1是5x1,op2是3x1)

我甚至不理解错误信息。我的错误在哪里?

解决方案

好的,找到它了。我希望教程告诉我这个。

  Y = []; 
for i = unique(y)',
%^
%------------- /
Y = [Y y == i ]。
end

显然,从一个向量的循环从左到右, unique 返回一个列向量,所以nonconformant arguments (5×1)和 unique(y)(3 x 1)。$ / $ c

I'm trying to get a binary one-of-K coding of a integer vector in Octave. I've got a vector y, say

y = [1 ; 2 ; 3 ; 1 ; 3]

and I want a matrix

Y = [1 0 0
     0 1 0
     0 0 1
     1 0 0
     0 0 1]

I can construct the one-of-K matrix by hand with

Y = [];
Y = [Y y == 1];
Y = [Y y == 2];
Y = [Y y == 3];

But when I try to construct it with a for loop,

Y = [];
for i = unique(y),
    Y = [Y y == i];
endfor

something goes wrong:

error: mx_el_eq: nonconformant arguments (op1 is 5x1, op2 is 3x1)

I don't even understand the error message. Where's my mistake?

解决方案

Ok, found it. I wish the tutorial had told me this.

Y = [];
for i = unique(y)',
%                ^
%  -------------/
    Y = [Y y == i];
end

Apparently, for loops through the columns of a vector from left to right and unique returns a column vector, so the "nonconformant arguments" are y (5×1) and unique(y) (3×1).

这篇关于Octave中的1-K编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆