I am struggling with some basic vectorization operations in Octave.
Lets say I instantiate a 10*10 matrix A.
A = magic(10)
I also instantiate a vector 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.
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
. But I am unsure, whether other options exist to vectorize this loop.
v = A * x
? Looks like a perfect solution. – Danielsum(A.*x.', 2)
, which uses element-wise multiplication with broadcasting. But matrix multiplication is likely to be a little faster, and it looks cleaner – Luis Mendo