Vectorization

조회 수: 2 (최근 30일)
Alan Shillitoe
Alan Shillitoe 2012년 5월 21일
Hi all,
Answer please in terms of 'Vectorization For Dummies'.
If I have a nested for loop of the form:
for i = 1:10
for j = 1:10
for k = 1:10
array(i,j,k) = function(input1(i), input2(j), input3(k))
end
end
end
What is the shortened codeneed to use to vectorize this?
Thanks
Alan
  댓글 수: 3
Oleg Komarov
Oleg Komarov 2012년 5월 21일
The answer would be to vectorize the function such that it accepts arrays instead of scalars.
Daniel Shub
Daniel Shub 2012년 5월 21일
I believe the speed up to loops has been around since the introduction of the JIT (maybe MATLAB 6.5). TMW tends not to give details of the JIT because it is a moving target. The profiler also does not use the JIT so timing becomes problematic. One advantage of loops is you can use parfor loops and take advantage of all you CPUs/cores.

댓글을 달려면 로그인하십시오.

답변 (4개)

Jan
Jan 2012년 5월 22일
No new answer, but more explicit:
Very slow:
for i = 1:100
for j = 1:100
for k = 1:100
array(i,j,k) = sin(i+j+k);
end
end
end
Faster with pre-allocation:
array = zeros(100,100,100);
for i = 1:100
for j = 1:100
for k = 1:100
array(i,j,k) = sin(i+j+k);
end
end
end
Fast partial vectorization:
array = zeros(100,100,100);
for i = 1:100
for j = 1:100
array(i,j,1:100) = sin(i+j+(1:100));
end
end
Full vectorization:
value = bsxfun(@plus, bsxfun(@plus, 1:100, transpose(1:100)), reshape(1:100, 1, 1, 100));
array = sin(value);
For large problems the creation of the large temporary arrays needs more time that the vectorized operation can save. Therefore the full vectorization is optimal for expensive functions, which have a lot of overhead for checking of inputs etc.
  댓글 수: 2
Alan Shillitoe
Alan Shillitoe 2012년 5월 22일
Thanks Jan.
That is the sort of thing I was after. I'll have a closer look at bsxfun to understand it better.
A.
Daniel Shub
Daniel Shub 2012년 5월 22일
+1, but why not make the outer loop a parfor?

댓글을 달려면 로그인하십시오.


Alan Shillitoe
Alan Shillitoe 2012년 5월 21일
Aren't they? This is the first I have read about this. Have you got a link to this in the documentation?
A.
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2012년 5월 21일
Please use comments.

댓글을 달려면 로그인하십시오.


Alan Shillitoe
Alan Shillitoe 2012년 5월 21일
Oleg,
How do I do that? I'm pretty new to this, so need it in really basic terms.
A.
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2012년 5월 21일
Edit your original question and post the relevant code of the function.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 5월 21일
Loops are no longer as slow in MATLAB, but function calls are. You want to reduce the number of function calls, which you do by rewriting the function itself to be vectorized. If you cannot do that, then there is effectively no available speed-up for your current code (assuming you have pre-allocated the output array)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by