我可以使用'=='运算符和'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) 如何1有效地设置AVX2
N位N位的__m256i,其余的设置0?
当范围可以在__m256i值的中间开始和结束时,这些是针对位范围的尾部和头部的2个单独的操作.占据全部__m256i值的范围部分使用全部0或全部1掩模进行处理.
我正在寻找一个超级数字正交函数.它应该具有以下三个属性:
另外,它应该是:
有人知道有这样一个函数的库吗?即使四个属性中的两个或三个也不会好.
我正在使用Python和SciPy,所以如果它已经与Python一起使用,那就是奖励.(但是我也可以编写胶水代码,让它在必要时调用我的被积函数.)
我有一个M x N矩阵.我想N用M 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) 我正在尝试将我的代码设置为自动向量化,但它无法正常工作.
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) 我的数据有一列,我正在尝试使用行中每个"/"之后的内容创建其他列.以下是数据的前几行:
> 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) 我有一个函数,它接收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是否可行.
我有两列我存储在我的数据帧中.
我想使用快速矢量化操作在两列上执行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在两列中都输入了.
有一个很好的解决方案来克服这个问题吗
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循环需要永远,我需要运行这个代码数万次."
谢谢!
我有两个数据框,下面是每个的一个小样本:
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的行数不同.
但是,我真正想要的是避免"按行"这样做,因为我的数据框有很多行.有没有办法矢量化?