How can I more fully vectorize to eliminate for loops?
    5 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Christopher Smith
 il 26 Nov 2022
  
    
    
    
    
    Commentato: Christopher Smith
 il 26 Nov 2022
            I have three vectors that must be comined to form a forth vector as  where
 where  .  I want to completely vectorize this computation to save time since these three vectors can become large.  I have done half of the vectorization to eliminate a for-loop for the j-index by employing the sum function and .* functionality, but I can't figure out the last bit to get rid of the for-loop for the i-index (see the sample code below).  The time savings with the partially vectorized version is substantial, but hoping for more with further vectorization.  One last thing to note is that this situation is a part of a marching scheme, which means that a "parfor" loop for the i-index would probably not work.  Any help would be appreciated!
.  I want to completely vectorize this computation to save time since these three vectors can become large.  I have done half of the vectorization to eliminate a for-loop for the j-index by employing the sum function and .* functionality, but I can't figure out the last bit to get rid of the for-loop for the i-index (see the sample code below).  The time savings with the partially vectorized version is substantial, but hoping for more with further vectorization.  One last thing to note is that this situation is a part of a marching scheme, which means that a "parfor" loop for the i-index would probably not work.  Any help would be appreciated!
 where
 where  .  I want to completely vectorize this computation to save time since these three vectors can become large.  I have done half of the vectorization to eliminate a for-loop for the j-index by employing the sum function and .* functionality, but I can't figure out the last bit to get rid of the for-loop for the i-index (see the sample code below).  The time savings with the partially vectorized version is substantial, but hoping for more with further vectorization.  One last thing to note is that this situation is a part of a marching scheme, which means that a "parfor" loop for the i-index would probably not work.  Any help would be appreciated!
.  I want to completely vectorize this computation to save time since these three vectors can become large.  I have done half of the vectorization to eliminate a for-loop for the j-index by employing the sum function and .* functionality, but I can't figure out the last bit to get rid of the for-loop for the i-index (see the sample code below).  The time savings with the partially vectorized version is substantial, but hoping for more with further vectorization.  One last thing to note is that this situation is a part of a marching scheme, which means that a "parfor" loop for the i-index would probably not work.  Any help would be appreciated!V = linspace(1,10,100);
G = linspace(1,10,100);
W = linspace(11,20,500);
Wans = 0*W;
Wans2 = 0*W;
tic
% For-loop version
for i = 1:length(W) 
    sumdum = 0;
    for j = 1:length(V)
        sumdum = G(j)*(W(i)-V(j));
        Wans(i) = Wans(i) + sumdum;
    end
end
toc
% Partially Vectorized version
tic
for i = 1:length(W)
    Wans2(i) = sum(G.*(W(i)-V));
end
toc
% Check to make sure the partially vectorized version gives the same answer
% as the for-loop version, display the difference if there is one
dW = diff([Wans;Wans2],1);
if any(abs(dW) > 1e-3)
    dW
end
% Fully vectorized version is hopefully a single line...
0 Commenti
Risposta accettata
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
			
	Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

