Loop over a list of variables indexed by numbers

2 vues (au cours des 30 derniers jours)
alpedhuez
alpedhuez le 14 Août 2021
Commenté : alpedhuez le 14 Août 2021
Suppose I have a table
Day1Vistors Day1Sales Day2Visitors Day2Sales
NY 10 100 15 45
SF 5 20 10 50
Then I want to calculate Day1SalesPerVisitor, Day2SalesPerVisitor
I would lke to write a loop over variable names like
for i=1:2
DayiSalesPerVisitor=DayiSales./DayiVisitor;
end
How can one operationalize it?

Réponse acceptée

Matt J
Matt J le 14 Août 2021
Modifié(e) : Matt J le 14 Août 2021
Where your table is T,
for i=1:2
DaySalesPerVisitor{i}=T{:,2*i}./T{:,2*i-1};
end
Alternatively,
A=table2array(T);
DaySalesPerVisitor=A(:,2:2:end)./A(:,1:2:end);
  1 commentaire
alpedhuez
alpedhuez le 14 Août 2021
Wanted more general method to run loop over variable names but this would surely work.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by