Effacer les filtres
Effacer les filtres

How can I write this with a for loop?

2 vues (au cours des 30 derniers jours)
SAHIL SAHOO
SAHIL SAHOO le 14 Fév 2023
Modifié(e) : Dyuman Joshi le 14 Fév 2023
r = (1/70).*( exp(i.*Y(:,3)) + exp(i.*Y(:,6)) + exp(i.*Y(:,9)) + exp(i.*Y(:,12)) + exp(i.*Y(:,15)) ...
+exp(i.*Y(:,18)) +exp(i.*Y(:,21)) +exp(i.*Y(:,24)) + exp(i.*Y(:,27)) + exp(i.*Y(:,30)) + exp(i.*Y(:,33)) ...
+ exp(i.*Y(:,36)) + exp(i.*Y(:,39)) +exp(i.*Y(:,42)) + exp(i.*Y(:,45)) + exp(i.*Y(:,48)) + exp(i.*Y(:,51)) ...
+ exp(i.*Y(:,54))+ exp(i.*Y(:,57)) + exp(i.*Y(:,60)) + exp(i.*Y(:,63)) + exp(i.*Y(:,66)) + exp(i.*Y(:,69)) ...
+ exp(i.*Y(:,72)) + exp(i.*Y(:,75)) + exp(i.*Y(:,78)) + exp(i.*Y(:,81)) + exp(i.*Y(:,84)) + exp(i.*Y(:,87)) ...
+ exp(i.*Y(:,90)) + exp(i.*Y(:,93)) + exp(i.*Y(:,96)) + exp(i.*Y(:,99)) + exp(i.*Y(:,102)) + exp(i.*Y(:,105))...
+ exp(i.*Y(:,108)) + exp(i.*Y(:,111)) + exp(i.*Y(:,114))+ exp(i.*Y(:,117)) + exp(i.*Y(:,120)) + exp(i.*Y(:,123))...
+ exp(i.*Y(:,126)) + exp(i.*Y(:,129)) + exp(i.*Y(:,132)) + exp(i.*Y(:,135)) + exp(i.*Y(:,138)) + exp(i.*Y(:,141))...
+ exp(i.*Y(:,144)) + exp(i.*Y(:,147)) + exp(i.*Y(:,150)) + exp(i.*Y(:,153)) + exp(i.*Y(:,156)) + exp(i.*Y(:,159)) ...
+ exp(i.*Y(:,162)) + exp(i.*Y(:,165)) + exp(i.*Y(:,168)) + exp(i.*Y(:,171)) + exp(i.*Y(:,174)) + exp(i.*Y(:,177)) ...
+ exp(i.*Y(:,180)) + exp(i.*Y(:,183))+ exp(i.*Y(:,186)) + exp(i.*Y(:,189)) + exp(i.*Y(:,192)) + exp(i.*Y(:,195)) ...
+ exp(i.*Y(:,198)) + exp(i.*Y(:,201))+ exp(i.*Y(:,204)) + exp(i.*Y(:,207)) + exp(i.*Y(:,210)));
  1 commentaire
Stephen23
Stephen23 le 14 Fév 2023
Why do you need to use a loop? Vectorized code would be simpler:

Connectez-vous pour commenter.

Réponses (2)

Dyuman Joshi
Dyuman Joshi le 14 Fév 2023
Modifié(e) : Dyuman Joshi le 14 Fév 2023
Vectorization would be the best approach here -
r = (1./70).*sum(exp(i*Y(:,3:3:210)),2)

Dr. W. Kurt Dobson
Dr. W. Kurt Dobson le 14 Fév 2023
Looks like the index you are using starts with 3, then increments by 3 up to 210.
Y looks like a matrix...
So, let's say the incrementing number is k, therefore you could do a for loop or just a vector computation.
For Loop Version (slower due to loop)
%
r = (1/70).*( exp(i.*Y(:,3)); % setup the first term
for k = 6:3:210 % increment by three
r = r + exp(i.*Y(:,k)); % for each term add, the prior to current
end
%
Vector Version (much faster)
k = 6:3:210; % gen k vector start at 3, increment by 6 to 210
r = (1/70).*( exp(i.*Y(:,3)); % setup the first term
r = r + exp(i.*Y(:,k)); % vector calculation on remaining terms
  1 commentaire
Dr. W. Kurt Dobson
Dr. W. Kurt Dobson le 14 Fév 2023
Oops, forgot a semicolon,
k = 6:3:210; % gen k vector start at 3, increment by 6 to 210
r = (1/70).*( exp(i.*Y(:,3))); % setup the first term
r = r + exp(i.*Y(:,k)); %

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by