标签: vectorization

Octave/Matlab:vectorising'=='运算符?

我可以使用'=='运算符和'find()'函数在向量'data'中查找值的位置,即45的位置:

data = [ 71 65 23 45 34 12 21 34 52 ];
value = 45;

find (data == value)
ans =  4
Run Code Online (Sandbox Code Playgroud)

有没有办法在不使用循环的情况下为几个值做同样的事情,即我想在一次调用中得到[4 5 7]:

values = [ 45 34 21 ];
find (data == values)
error: mx_el_eq: nonconformant arguments (op1 is 1x9, op2 is 1x3)
error: evaluating argument list element number 1
error: evaluating argument list element number 1
Run Code Online (Sandbox Code Playgroud)

matlab operators vectorization find octave

8
推荐指数
1
解决办法
2084
查看次数

将__m256i的前N位或后N位设置为1的有效方法,其余为0

如何1有效地设置AVX2

  1. 第一N
  2. 最后N

__m256i,其余的设置0

当范围可以在__m256i值的中间开始和结束时,这些是针对位范围的尾部和头部的2个单独的操作.占据全部__m256i值的范围部分使用全部0或全部1掩模进行处理.

c++ bit-manipulation x86-64 vectorization avx2

8
推荐指数
1
解决办法
734
查看次数

Python中的数值积分与向量化函数的自适应求积法

我正在寻找一个超级数字正交函数.它应该具有以下三个属性:

  • 自适应 - 它会自动调整采样点的密度以适应被积函数.这是绝对必要的,因为我的被积函数非常不均匀且计算成本很高.
  • 矢量化 - 为了提高效率,它会在采样点列表上调用被积函数,而不是一次调用一个点.
  • 能够处理向量值函数 - 向量值被积函数的所有组件都是同时计算的,无需额外成本,因此将所有组件分别集成是没有意义的.

另外,它应该是:

  • 2D - 我想要计算的积分是平面区域上的双积分,我希望能够指定整个积分的整体(相对)容差,并让它适当地管理误差预算.

有人知道有这样一个函数的库吗?即使四个属性中的两个或三个也不会好.

我正在使用Python和SciPy,所以如果它已经与Python一起使用,那就是奖励.(但是我也可以编写胶水代码,让它在必要时调用我的被积函数.)

python numpy vectorization scipy numerical-integration

7
推荐指数
2
解决办法
2727
查看次数

将矩阵的每列乘以另一个矩阵

我有一个M x N矩阵.我想NM x M矩阵乘以每列.以下是循环中的这个,但我不知道如何对其进行矢量化.

 u=repmat(sin(2*pi*f*t),[n 1]);
 W = rand(n);
 answer = size(u);
 for i=1:size(u,2)
   answer(:,i) = W*u(:,i);
 end
Run Code Online (Sandbox Code Playgroud)

matlab matrix vectorization matrix-multiplication

7
推荐指数
1
解决办法
3690
查看次数

自动矢量化不起作用

我正在尝试将我的代码设置为自动向量化,但它无法正常工作.

int _tmain(int argc, _TCHAR* argv[])
{
    const int N = 4096;
    float x[N];
    float y[N];
    float sum = 0;

    //create random values for x and y 
    for (int i = 0; i < N; i++)
    {
        x[i] = rand() >> 1;
        y[i] = rand() >> 1;
    }

    for (int i = 0; i < N; i++){
        sum += x[i] * y[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

这里没有循环矢量化,但我真的只对第二个循环感兴趣.

我正在使用visual studio express 2013并且正在编译/O2/Qvec-report:2(报告循环是否被矢量化)选项.编译时,我收到以下消息:

--- Analyzing function: main
c:\users\...\documents\visual studio 2013\projects\intrin3\intrin3\intrin3.cpp(28) …
Run Code Online (Sandbox Code Playgroud)

c++ optimization sse simd vectorization

7
推荐指数
1
解决办法
2439
查看次数

拆分字符串列以创建新的二进制列

我的数据有一列,我正在尝试使用行中每个"/"之后的内容创建其他列.以下是数据的前几行:

> dput(mydata)
structure(list(ALL = structure(c(1L, 4L, 4L, 3L, 2L), .Label = c("/
ca/put/sent_1/fe.gr/eq2_on/eq2_off",
"/ca/put/sent_1/fe.gr/eq2_on/eq2_off/cbr_LBL", "/ca/put/sent_1/fe.g
r/eq2_on/eq2_off/cni_at.p3x.4",
"/ca/put/sent_1/fe.gr/eq2_on/eq2_off/hi.on/hi.ov"), class = "factor
")), .Names = "ALL", class = "data.frame", row.names = c(NA,
-5L))
Run Code Online (Sandbox Code Playgroud)

如果变量出现在行中,结果应该如此(数据框)在新列中带有"1",否则为"0":

> dput(Result)
structure(list(ALL = structure(c(1L, 4L, 5L, 3L, 2L), .Label = c("/ca
/put/sent_1/fe.gr/eq2_on/eq2_off",
"/ca/put/sent_1/fe.gr/eq2_on/eq2_off/cbr_LBL", "/ca/put/sent_1/fe.gr/
eq2_on/eq2_off/cni_at.p3x.4",
"/ca/put/sent_1/fe.gr/eq2_on/eq2_off/hi.on/hi.ov", "/ca/put/sent_1fe.
gr/eq2_on/eq2_off/hi.on/hi.ov"
), class = "factor"), ca = c(1L, 1L, 1L, 1L, 1L), put = c(1L,
1L, 1L, 1L, 1L), sent_1 = c(1L, 1L, 1L, 1L, 1L), fe.gr = c(1L, …
Run Code Online (Sandbox Code Playgroud)

split r vectorization

7
推荐指数
2
解决办法
626
查看次数

Matlab - 单输出扩展两个以上的输入

我有一个函数,它接收2个以上的变量

例如.

testFunction = @(x1, x2, x3, x4) x1.*x2.*x3.*x4;
testFunction2 = @(x1, x2, x3) sin(x1.*x2.^x3);
Run Code Online (Sandbox Code Playgroud)

有没有可用的功能bsxfun允许单个扩展功能有多于2个输入?

的例子 bsxfun

binaryTestFunction = @(x1, x2) x1.*x2;
x1 = 9*eye(10);
x2 = 3*ones(10,1);
A = bsxfun(binaryTestFunction , x1 , x2);
Run Code Online (Sandbox Code Playgroud)

扩展x2向量中的单例维度.
bsxfun比repmat更快,也是高度迭代的,因此我想知道单例扩展testFunction是否可行.

matlab vectorization bsxfun

7
推荐指数
1
解决办法
272
查看次数

在熊猫中设置联盟

我有两列我存储在我的数据帧中.

我想使用快速矢量化操作在两列上执行set union

df['union'] = df.set1 | df.set2
Run Code Online (Sandbox Code Playgroud)

但错误TypeError: unsupported operand type(s) for |: 'set' and 'bool'阻止我这样做,因为我np.nan在两列中都输入了.

有一个很好的解决方案来克服这个问题吗

python numpy vectorization python-3.x pandas

7
推荐指数
2
解决办法
4717
查看次数

在R中使用唯一函数矢量化for循环

player_ids = c(34, 87, 27, 34, 87, 9, 29, 25, 24, 25, 34, 37)
end = length(player_ids)
unique_players_list = list()

for(i in 1:end) {
  unique_players_list[[i]] = unique(player_ids_unlisted[1:i])
}
Run Code Online (Sandbox Code Playgroud)

这是我试图矢量化的for循环(缩短版本).我不知道如何发布代码输出,但是unique_players_list列表应该具有以下输出:

unique_players_list[[1]] == c(34)
unique_players_list[[2]] == c(34)
unique_players_list[[3]] == c(34, 87)
unique_players_list[[4]] == c(34, 87, 27)     
unique_players_list[[5]] == c(34, 87, 27)
Run Code Online (Sandbox Code Playgroud)

"等等.输出不必在列表中,我实际上更喜欢数据帧,但是我需要这个矢量化,因为我当前的for循环需要永远,我需要运行这个代码数万次."

谢谢!

loops r list unique vectorization

7
推荐指数
3
解决办法
589
查看次数

矢量地找到数据帧行的邻居

我有两个数据框,下面是每个的一个小样本:

df1 <- data.frame(a1= c(3,4), a2 = c(8, 8), a3 = c(4, 18), a4 = c(9,9), a5 = c(17, 30))

df2 <- data.frame(a1 = c(2,2,2,3,3,3,4,4,4), a2 = c(7,7,7,7,7,7,7,7,7), 
                 a3 = c(4,4,4,4,4,4,4,4,4), a4 = c(10,10,10, 10, 10, 10, 10,10,10), 
                 a5 = c(15,16,17, 15, 16, 17, 15, 16, 17))
Run Code Online (Sandbox Code Playgroud)

我想检查,对于每一行df1,它是否有"邻居" df2,其中,邻居我的意思是每列中最多1个(绝对值)不同的观察.因此,例如,第2行df2是第1行的邻居df1.

我目前这样做的方式如下:

sweep(as.matrix(df2), 2, as.matrix(df1[1,]), "-")
Run Code Online (Sandbox Code Playgroud)

对于第1行df1,我必须为df1的每一行重复此操作.请注意,df2和df1的行数不同.

但是,我真正想要的是避免"按行"这样做,因为我的数据框有很多行.有没有办法矢量化?

r vectorization dataframe

7
推荐指数
1
解决办法
96
查看次数