这是一个关于在同一语句中多次递增MATLAB数组的一个值的问题,而不必使用for循环.
我将我的数组设置为:
>> A = [10 20 30];
Run Code Online (Sandbox Code Playgroud)
然后运行:
>> A([1, 1]) = A([1, 1]) + [20 3]
A =
13 20 30
Run Code Online (Sandbox Code Playgroud)
显然,20被忽略了.但是,我希望它被包括在内,以便:
>> A = [10 20 30];
>> A([1, 1]) = A([1, 1]) + [20, 3]
Run Code Online (Sandbox Code Playgroud)
会给:
A =
33 20 30
Run Code Online (Sandbox Code Playgroud)
是否有一个函数允许以一种漂亮的矢量化方式完成它?
(实际上,对数组的索引将包括多个索引,因此它可能是[1 1 2 2 1 1 1 1 3 3 3]等等,并且数字数组将以[20, 3]相同的长度递增(以上).)
循环向量化是在开始时计算所有右侧表达式的时间.我刚刚发现我的循环正在被矢量化(在FORTRAN 77中......不要问).我需要在每次迭代中更新循环条件变量,但是如何重写以解决此向量化?
在一篇相关文章中,我正在寻找一种方法来专门禁用FORTRAN中的这种优化"功能",但在这里我正在寻找一种更通用的算法解决方案.
我有一个d带向量参数的维度函数,我试图在一个简单的情况下在常规网格上计算它的值d=2.outer在这里尝试是很自然的,它可以很好地工作,即在简单的情况下
> outer(1:2, 3:4, function(x, y) x+y)
[,1] [,2]
[1,] 4 5
[2,] 5 6
Run Code Online (Sandbox Code Playgroud)
但是,我的功能不支持自然矢量化.为了说明,想一想像
> outer(1:2, 3:4, function(x, y) length(c(x, y)))
Run Code Online (Sandbox Code Playgroud)
具有所需的输出(显然,上述代码的实际结果是错误)
[,1] [,2]
[1,] 2 2
[2,] 2 2
Run Code Online (Sandbox Code Playgroud)
我当前的解决方法有点像apply(expand.grid(1:2, 3:4), 1, length),但对我来说这看起来有点笨拙.有没有像outer这种情况一样简单的事情?
我需要一个快速的方法在Matlab中做这样的事情(我正在处理大量的向量,所以一个正常的循环需要永远!):
从像矢量
[0 0 2 3 0 0 0 5 0 0 7 0]
Run Code Online (Sandbox Code Playgroud)
我需要得到这个:
[NaN NaN 2 3 3 3 3 5 5 5 7 7]
Run Code Online (Sandbox Code Playgroud)
基本上,每个零值被替换为先前非零值的值.第一个是NaN因为向量中没有先前的非零元素.
我有2个numpy数组:
aa = np.random.rand(5,5)
bb = np.random.rand(5,5)
Run Code Online (Sandbox Code Playgroud)
当aa和bb都超过0.5时,如何创建一个值为1的新数组?
我想找到n可以在m箱子之间拆分物品的所有方法.例如,for n=3和m=3输出将是(顺序无关紧要):
[3 0 0
0 3 0
0 0 3
2 1 0
1 2 0
0 1 2
0 2 1
1 0 2
2 0 1
1 1 1]
Run Code Online (Sandbox Code Playgroud)
算法应该尽可能高效,最好是矢量化/使用内置函数而不是for循环.谢谢!
给定一个数组,我想将其标准化,使每行总和为1.
我目前有以下代码:
import numpy
w = numpy.array([[0, 1, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 0]], dtype = float)
def rownormalize(array):
i = 0
for row in array:
array[i,:] = array[i,:]/sum(row)
i += 1
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
1)代码有效,但我想知道是否有更优雅的方式.
2)如果它是int,我如何将数据类型转换为float?我试过了
if array.dtype == int:
array.dtype = float
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我有一个大约10,000,000行的数据帧,需要对其中一列进行操作.列中唯一值的数量大约低两个数量级,所以目前我正在通过应用memoized函数进行转换.
new = [foo(x) for x in df.column])
index = np.where(new > df.other, new, df.other)
df.set_index(index)
@memoized
def foo(x):
if x > 0:
bar = -1
else:
bar = 10
x *= bar
return x
Run Code Online (Sandbox Code Playgroud)
数据帧的庞大规模意味着计算new仍然比我想要的时间更长.
有没有办法使用vecorization加快这一步?还是其他任何不矢量化的技巧?
I am trying to understand the function stats::mahalanobis. Here is its source, but please just focus on last line, or more specifically, the rowSums(x %*% cov * x) part.
> mahalanobis
function (x, center, cov, inverted = FALSE, ...)
{
x <- if (is.vector(x))
matrix(x, ncol = length(x))
else as.matrix(x)
if (!isFALSE(center))
x <- sweep(x, 2L, center)
if (!inverted)
cov <- solve(cov, ...)
setNames(rowSums(x %*% cov * x), rownames(x))
}
Run Code Online (Sandbox Code Playgroud)
Here x is a n-by-p matrix, whereas cov is …
我正在尝试对数学模型进行编码,它涉及在数千个具有变化的模型参数的值网格上计算特定数量。目前,这太慢了,我正在寻找有关向量化模型中最密集部分的建议。
为了便于阅读,我目前已经有了它的基本实现,但是现在如果可能的话,希望对下面的整个代码段进行矢量化处理。该代码段的一个最小示例是:
% Setup grid to evaluate and results vector
T_max = 10000;
eval_points = linspace(0, T_max, 1000);
results = zeros(size(eval_points));
% Function that is used in computation
Z_func = @(x, omega) (1./(omega.*sqrt(2*pi))).*exp( -(x.^2)./(2.*omega.*omega) );
% Random data for now, known in full problem
historic_weights = rand(1,100);
historic_times = rand(1,100);
% Fixed single parameter omega
omega = 0.5;
% Time evaluation
tic()
for eval_counter = 1:size(eval_points,2)
for historic_counter = 1:size(historic_weights,2)
temp_result = 0;
for k = 0:1:T_max
temp_result = temp_result …Run Code Online (Sandbox Code Playgroud) vectorization ×10
matlab ×4
numpy ×3
python ×3
performance ×2
r ×2
apply ×1
arrays ×1
combinations ×1
fortran ×1
increment ×1
indexing ×1
loops ×1
math ×1
matrix ×1
pandas ×1
permutation ×1