简体   繁体   English

矢量化八度

[英]Vectorization Octave

I am struggling with some basic vectorization operations in Octave.我正在努力解决 Octave 中的一些基本矢量化操作。

Lets say I instantiate a 10*10 matrix A. A = magic(10) I also instantiate a vector x.假设我实例化了一个 10*10 矩阵 A。A A = magic(10)我还实例化了一个向量 x。 x = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

I want to use a vectorized operation, instead of for loops to fill in an empty vector e.我想使用向量化操作,而不是 for 循环来填充空向量 e。 e = zeros(10,1)

for i = 1:10
  for j = 1:10
    v(i) = v(i) + A(i, j) * x(j);
  end
end

I have studied the the octave documentation chapter 19 about vectorization, and I believe that the only answer is v = A * x .我研究了有关矢量化的 octave 文档第 19 章,我相信唯一的答案是v = A * x But I am unsure, whether other options exist to vectorize this loop.但我不确定是否存在其他选项来矢量化这个循环。

Using multiplication is the best option, but there are other options, for example:使用乘法是最好的选择,但还有其他选择,例如:

sum(A.*x.',2)

You often find such solutions when it comes to vectorizing loops like:当涉及到矢量化循环时,您经常会找到这样的解决方案,例如:

for i = 1:10
  for j = 1:10
    v(i) = v(i) + f(i,j);
  end
end

The intermediate step is a matrix which holds all solutions for f(i,j) ( sum(A.*x.',2) in your example), then you accumulate replacing the + with a sum .中间步骤是一个矩阵,其中包含 f(i,j) 的所有解决方案(在您的示例中为sum(A.*x.',2) ),然后您累积将+替换为sum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM