How to vectorize 'for' loop in this code?

조회 수: 1 (최근 30일)
Mr. 206
Mr. 206 2018년 11월 22일
편집: madhan ravi 2018년 11월 22일
My code is like this...
c = 0;
for j = 3: 2 : size(READ,2)
c = c+1;
output(c) = .....
end
I want avoid 'c' and 'for loop' in my code but as you are seeing it is necessary to store the results of loop.
I want it to be like this
j = 3: 2 : size(READ,2)
output = ....
But in this case all the outputs are not stored. I want this thing to make my code faster.
  댓글 수: 2
Jan
Jan 2018년 11월 22일
편집: Jan 2018년 11월 22일
There is no benefit in vectorizing in general. Depending on what is hidden in ".....", a vectorized version can be slower or impossible to create. So please post this very important detail.
Using the index c is nice and efficient, if you pre-allocate the array output.
Mr. 206
Mr. 206 2018년 11월 22일
Thanks.
I thought vectoring will make my code faster.

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

답변 (1개)

madhan ravi
madhan ravi 2018년 11월 22일
You can preallocate output before the loop but to store the output your first way is the only way.
  댓글 수: 1
Mr. 206
Mr. 206 2018년 11월 22일
편집: madhan ravi 2018년 11월 22일
Thanks.
I will do so .

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by