有没有办法在matlab / octave中对这段代码进行矢量化?

时间:2016-10-17 23:14:34

标签: matlab octave vectorization

有没有办法对这段代码进行矢量化?

for k=1:K
    vec_y(:,k) = y == k;
end

此处,y3000x1维度的向量,并且具有元素110(表示),使得每个表示与每个其他表示具有相同的计数(即,300计数1300计数2等。

我想要做的是重写或只创建一个新的矩阵,对于所有出现的相应1 - 10具有以下二进制表示形式:

1将由[1;0;0;0;0;0;0;0;0;0]代表,2将由[0;1;0;0;0;0;0;0;0;0]代表,依此类推。

我想在不使用for循环的情况下完全向量化代码。

1 个答案:

答案 0 :(得分:3)

按照@Suever

的建议

编辑

一种可能性:

['/home/peng/git/datapassport/spookystuff/mav', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/peng/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
---------------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 240, in serve_client
    request = recv()
  File "/home/peng/git/datapassport/spookystuff/mav/pyspookystuff/mav/comm/__init__.py", line 10, in <module>
    from pyspookystuff.mav import utils, mpManager
ImportError: cannot import name mpManager
---------------------------------------------------------------------------