How to vectorize 'for' loop in this code?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 22 Nov. 2018
Bearbeitet: Jan
am 22 Nov. 2018
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.
Antworten (1)
madhan ravi
am 22 Nov. 2018
You can preallocate output before the loop but to store the output your first way is the only way.
1 Kommentar
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!